We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    The trick is that one Main object creates other Child objects... but how to refer back to the Main’s methods and properties? Well, pass by reference. The below code works... my concern is that if the $this object is huge, am I doing this correctly so that I don’t copy it each time I instantiate a child object?

    <?php
    
    class Main {
            var $x = 3;
            function a(){
                    $this->Child = new Child(&$this);
            }
    }
    
    class Child {
            function Child(&$m){
    
                    print $m->x;
            }
    }
    
    $Main = new Main();
    
    $Main->a();
    
    
    ?>
      • 9130
      • 171 Posts
      You are doing it right.

      Actually you don’t need to specifically pass a reference in the function call itself, just having the reference in the function definition in enough.

        • 9207 ☆ A M B ☆
        • 2,475 Posts
        Just to make sure I’m following you... which of the above lines can omit the ’&’?
          • 26903
          • 1,336 Posts
          This one I believe
          $this->Child = new Child(&$this);
          . Child is already defined as having a reference parameter so you don’t need to specify it again on the call. I think. PHP5 warns you about this now.
            Use MODx, or the cat gets it!
            • 9130
            • 171 Posts
            exactly.
              • 28215
              • 4,149 Posts
              A little nicer code to what you’re proposing, using the Factory paradigm:

              class Main {
              	protected $number = 3;
              	
              	public function create($class) {
              		if (class_exists($class)) {
              			$this->$class = new $class($this);
              		}
              		return $this->$class;
              	}
              	
              	public function getNumber() {
              		return $this->number;
              	}
              }
              
              class Child {
              	function __construct(Main &$main) {
              		$this->main =& $main;
              		print $this->main->getNumber();
              	}
              }
              
              $main = new Main();
              $main->create('Child');
              


              You could also go further, and override __get and __set to not need the getNumber method, but alas. tongue
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                THANK YOU! Wow, it’s SOOOO helpful to have input on this stuff. I had this nasty intuition that how I was doing it wasn’t right, but it’s so hard to dig through the examples to find the one that matches what I’m trying to do. This is how I learn new techniques. DESIGN PATTERNS, HERE I COME!
                  • 9207 ☆ A M B ☆
                  • 2,475 Posts
                  One more question... if you want to pass additional arguments to the class, how do you do that when you’ve got that inheritance thing going on in there.


                  AAaaaand, since we’re talking design, how do you feel about config files? I’ve seen CakePHP’s version:

                  class SOME_CONFIG {
                     var $x = 'something';
                  }


                  and CodeIgniter’s:

                  $config['x'] = 'something';


                  Presumably, CodeIgniter controls the exact location of that array by controller WHERE the config file is included. CakePHP’s approach is easier in many ways (and offers OO benefits), but iterating over config details (e.g. to look for conflicts) is more difficult.
                    • 9207 ☆ A M B ☆
                    • 2,475 Posts
                    Does this method work for extending the $modx object? (Versions 1 and earlier).
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Quote from: Everett at Nov 03, 2009, 05:23 PM

                      One more question... if you want to pass additional arguments to the class, how do you do that when you’ve got that inheritance thing going on in there.
                      Well, using a reference to a Main class from the Child class is not actually inheritance; it’s a reference. But generally, I would have an array parameter as part of the constructor for any class that could take any number of optional arguments. You can deal with them in the constructor of your class or save them as passed into a variable of your class for access by class methods.

                      Quote from: Everett at Nov 03, 2009, 09:19 PM

                      Does this method work for extending the $modx object? (Versions 1 and earlier).
                      Does what method work for "extending" the $modx object? I’m confused since there is no extending going on here...