We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 24714
    • 19 Posts
    What’s the difference between

    loadClass
    getService
    include (name of the file with class)

    I cant really get it =)
    Actually xPDO manual says that getService provides a convenience method for doing this in a single line...

    Didnt find it that simple =)
      • 22303 MODX Staff
      • 10,725 Posts
      loadClass() is used to load classes from any packages currently added to the xPDO instance. This is used internally by all of the methods to not only load the class files, but also to load the O/R map files that describe the data structures.

      getService() is used to load a "service" class, or a reusable class that accepts a reference to an xPDO instance as the first parameter. This is useful for loading reusable classes that might be used by multiple components in a single request. It also makes the service accessible from the xPDO instance using the specified variable key. A good example of this is the new modHashing class in MODX 2.1, which provides access to a simple message hashing class which is used by the core user system for hashing user passwords for storage:

      <?php
      /* get existing or create new modHashing instance and make it available as $modx->hashing */
      $modx->getService('hashing', 'hashing.modHashing');
      
      /* now you can simply ( re- )use the instance methods of the service class */
      $passwordHash = $modx->hashing->getHash('', $modx->user->get('hash_class'))->hash('easyas123', array('salt' => $modx->user->get('salt')));
      


      Does that help clarify the purposes of loadClass() and getService() in contrast to a simple PHP include/require?
        • 24714
        • 19 Posts
        in other words $modx->getService(’hashing’, ’hashing.modHashing’); does this ?

        $modx->hashing = &new modHashing; +it automatically extends modx class with modHashing???

          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: Redjik at May 13, 2011, 05:06 AM

          in other words $modx->getService(’hashing’, ’hashing.modHashing’); does this ?

          $modx->hashing = &new modHashing; +it automatically extends modx class with modHashing???
          Basically, except it does not extend the modX class, it simply passes a reference of $modx to the instance. Service classes generally have a $this->modx =& $modx in their constructor so the MODX instance is available to it’s methods.
            • 24714
            • 19 Posts
            Thx a lot, after spendin about 2-3 hours testing all these i ve figured out the same you have written =)
            Some more tests and i ll understand this model by 80 percents =)

            well i think i got the last question... as i’m stuck here.

            class mytest {
            		public $modx;
            		public $mycore,$mymanager;
            		
            		function __construct(&$modx,$mycore,$mymanager) {
            		$this->modx =& $modx;
            		
            		$mycore = $this->modx->getOption('core_path').'components';
                            $mymanager = $this->modx->getOption('manager_url').'components';
            		//echo $mycore.'</br>';
            				
            		}
            			
            	}
            


            So in construct i make a link to modx class. If uncomment the echo line? on class init i got the exact place of the core files, everything works fine.

            In my tests i initialized with include and loadClass, so i ll show loadclass instance.
            $modx->loadClass('mytest',$modx->getOption('core_path').'components/mycomp/class',false,true);
            $modxx = new mytest(&$modx);
            echo $modxx->manger.'</br>';
            echo $modxx->mycore.'</br>';
            

            so in my call i do the same thing as getService => reference to main xPDO class ($modx)

            so after all i get full way to my core (even including drive letter of my local server) in the 1st line
            in the second i dont get the full way although it is just ’components’
            int the 3d ’manager/components’

            why such a difference?



              • 24714
              • 19 Posts
              false alarm - everything works fine =)