We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34193
    • 330 Posts
    OK from what I can see there are 3 ways to use classes within MODX-Revo, what are the differences and which is the way forward, by way forward I mean will one way be depreciated in future realeases.

    way 1 (Standard PHP way)
    <?php 
    require_once 'path/classname.class.php';
    $instanceOfClass = new className($modx);
    ?>
    


    Way 2 (copied from docs)
    <?php
    $x = $modx->getService('extendeduser','Sampleclass',$modx->getOption('core_path',null, MODX_CORE_PATH).'components/extendeduser/',$scriptProperties);
    if (!($x instanceof Extendeduser)) {
        $modx->log(modX::LOG_LEVEL_ERROR,'[Extendeduser] Could not load Extendeduser class.');
        $modx->event->output(true);
    
    ?>
    


    Way 3 (Also taken direct from docs)
    <?php
    $modx->loadClass('myBox','/my/path/to/model/');
    ?>
    

      • 22303 MODX Staff
      • 10,725 Posts

      • way 1 is fine for including classes that have nothing to do with xPDOObject (i.e. don’t have maps), though there is no concept of packages with this method
      • way 2 is for loading a "service" as an object on the $modx instance, so you can do e.g. $modx->myservice->doSomething(), still not xPDOObject related (i.e. service classes do not have maps)
      • way 3 is for loading an xPDOObject representing a db table (i.e. does have a map), or loading other classes that do not have maps (i.e. are transient) from your xPDO package