We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4971
    • 964 Posts
    I am learning OOP programming and just beginning...

    I know that a single arrow like in this->property; is used to access
    a property or method on the same class.

    What I can’t find information about is the double arrow as in :
    $modx->resource->get(’pagetitle’);

    if I can guess, I would say that this is accesing the get method with the ’pagetitle’ argument
    on the resource class that is itself inside another class? the modx class?
      Website: www.mercologia.com
      MODX Revo Tutorials:  www.modxperience.com

      MODX Professional Partner
      • 5689
      • 289 Posts
      Your guess is absolutely right. $resource is a property of $modx, but the value of $resource is an object (an object is a specific instance of a class). So with that code, you’re accessing the get() method of the object stored in the $resource property of the $modx object.
        I'm learning more about MODx all the time and loving it.
        • 4971
        • 964 Posts
        Thanks Compeek

        $resource is a property or a method?

        so the code would be something like this?

        <?php
        class modx {
        	public $resource = object someObject;
        	
        	function modxFunction() {
        		$this->resource = someOtherObject;
        	}
        }
        
        class otherClass {
        	public $otherResource = 'otherStuff';
        	
        	function get(attributes) {
        		//  do something here
        	}
        }
        
        someObject = new otherClass;
        someValue = $modx->resource->get('someAttribute');
        ?>
        


        Thanks for your help...
          Website: www.mercologia.com
          MODX Revo Tutorials:  www.modxperience.com

          MODX Professional Partner
          • 5689
          • 289 Posts
          Mhm, $resource is a property, not a method.

          Your code has the general idea, but a few things aren’t quite right. I know it’s just an example, so we can’t be too specific, but here’s a few things just for general reference:

          First of all, all variables in PHP have a dollar sign for the first character. So instead of someObject, someValue, attributes, etc., you’d have $someObject, $someValue, $attributes, and so on. When you’re accessing a property, you drop the dollar sign (e.g. $modx->resource).
          Also, when you’re creating a new object, use parentheses as if you were calling a function. When you create an instance of a class (in other words, "new object()"), you’re actually also calling the constructor (lookup the __construct() function of classes), which accepts parameters. So you could have: "$someObject = new someClass(’some value for a parameter in the constructor’)"
          // You had this:
          someObject = new otherClass;
          // Should be this:
          $someObject = new otherClass();
          
          $someValue = $modx->resource->get('someAttribute');


          Now, variables declared outside a class are not accessible inside the class. Your variable declaration for $resource is trying to access the $someObject:
          public $resource = object someObject; // should be $someObject, by the way, and the word "object" is not necessary

          The problem is that you declared $someObject outside the class, and the class has no idea it exists. Think of classes as completely separate from the rest of your code. Classes are like templates. When you want to create an object, you create an instance of a class, which is like using the class as a template for your object. So no variables outside of a class can be access from inside a class because the class itself doesn’t know anything about the rest of your code.


          I think this code might be a better of example to explain what’s going on in your original question:
          <?php
          class modx {	
          	public $resource = new modxResource(); // that's not really what the resource class is called, but I don't know exactly what it is
          }
          
          class modxResource {
          	public function get($attribute) {
          		// get the attribute and return the value
          	}
          }
          
          
          $modx = new modx();
          
          $someValue = $modx->resource->get('someAttribute');
          ?>
          


          So what’s going on here is we have a variable called $modx that contains a modx object (the modx object is an instance of the modx class). The modx object itself has a property called $resource. $resource contains a resource object (which is an instance of the modxResource class). The get() method is part of the modxResource class.
          When you do "$modx->resource->get(’someAttribute’)", you’re calling the get() method on the resource object, which is stored as a property of the modx object.

          Let’s say you had just this:
          $resource = new modxResource();
          
          $someValue = $resource->get('someAttribute');
          

          Here you have a resource object, and you’re called its get() method. Now think about $resource being inside of the modx object. It becomes the $resource property, and everything is still exactly the same, except that now when you want to access it, you have to go through the modx object because the resource object is stored inside the modx object.

          One more thing that might clarify some of this. I don’t know how much PHP you know, so I don’t mean to assume you don’t know what you’re doing, but it’s just easier to be thorough.

          In PHP, a variable (which always has that $ in the name) can store any type of value, including arrays and objects.
          $someString = 'This is a string.';
          $someNumber = 1234;
          $someArray = array('dog', 'cat');
          $someObject = new someClass();

          $someObject contains an object, just like $someString contains a string.

          Properties of classes are still variables, and methods of classes are still functions. Since access a property with the arrow (and without the dollar sign), when you do something like "$modx->resource", you’re getting the object that’s stored in the $resource property. When you do "$resource->get()", you’re calling the get() method of the resource object stored in $resource. You put that all together into one line to get "$modx->resource->get()"

          I think I’m getting too wordy now and it might be confusing, so does that make sense?
            I&#39;m learning more about MODx all the time and loving it.
            • 5689
            • 289 Posts
            Oh, and here’s something else I just thought of that you might not know.

            Look at this code here:
            class someClass {	
            	public $someProperty = 'something';
            }

            When you put the equal sign and a value at the end when you’re declaring a property, you’re setting the initial value for that property. You don’t have to do that. If you don’t set an initial value, the property will just contain nothing until you give it a value. So you could have this, too:
            class someClass {	
            	public $someProperty;
            }


            The only reason I gave the $resource property an initial value was to give some merit to my example. In reality the modx class contains a whole lot more properties and methods, and since the resources and everything come from the database, the whole deal is a more complicated.
              I&#39;m learning more about MODx all the time and loving it.
              • 5689
              • 289 Posts
              The PHP manual (online) is one of the best of its kind, so I highly recommend reading through the section on OOP. It might take a while to get through it, but it’s a great start to understand what OOP is all about. http://php.net/manual/en/language.oop5.php
                I&#39;m learning more about MODx all the time and loving it.
                • 3749
                • 24,544 Posts
                Here’s another way to look at it.

                The $modx object has a member variable called $resource. That variable is set as a reference to the current resource when a page is requested.

                The $resource object has a method called get() that will get any of it’s fields by name.

                So --

                $modx->resource->get('id');


                is asking the resource object ( the object referenced by the $resource member variable of the $modx object) to use its get method to return its own ID field.

                One major part of OOP is that the objects contain their own member variables and methods. When you want to save an object, you call $object->save(). If you want it to sort itself, you call $object->sort(). If you want one of it’s member variables (in this case fields), you call $object->get(). If you want to set one of its fields, you call object->set().

                Because the $modx object extends the $xpdo object, you can ask it to retrieve any of the objects on the site (resources, members, elements, TVs, templates, etc.) with a $modx->get*() method. Often, once you have a reference to the object, you ask the object itself to get you something (e.g., its parent, its ID , its children, etc.). Each object represents a row in one of the database tables.

                $modx->getObject()   // get a single object
                $modx->getCollection()  // get an array of objects
                
                $modx->object->get() // get an object field
                $modx->object->getOne()  // get a single related object of the named object (e.g., Parent)
                $modx->object->getMany() // get multiple related objects of the names object (e.g., Children)
                



                There are two objects you don’t have to get. MODx gets the following two objects for you on every page request:

                $modx->resource // the current resource
                $modx->user // the current user
                


                Note that while get() generally returns a string with a field value, the other methods above return MODx objects that each have their own get(), getOne(), and getMany() methods.

                Hope this makes sense. smiley
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 4971
                  • 964 Posts
                  Compeek and Bobray, thanks a million for your time and knowledge...

                  It has helped me a lot... I have used PHP4 but now I am diving into PHP5 and OOP
                  so the original reason for my question is that as I learn, I check some MODx code
                  to se what is going on and I saw the "double arrow" hehehehe

                  I did not use the $ and parenthesis in the variables and functions for being lazy, sorry.

                  Yes both explanations, one in the general and the other in the MODx specific were clear
                  and will guide me while I study and practice OOP. Thanks for the link to the OOP section
                  of php.net

                  I already bookmarked the thread...

                  Thanks again, really appreciate it...
                    Website: www.mercologia.com
                    MODX Revo Tutorials:  www.modxperience.com

                    MODX Professional Partner