We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28215
    • 4,149 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.

    Like so:
    class Main {
    	protected $number = 3;
    	
    	public function create($class,array $config = array()) {
    		if (class_exists($class)) {
    			$this->$class = new $class($this,$config);
    		}
    		return $this->$class;
    	}
    	
    	public function getNumber() {
    		return $this->number;
    	}
    }
    
    class Child {
    	function __construct(Main &$main,array $config = array()) {
    		$this->main =& $main;
    		$this->config = array_merge(
    			/* custom child options here that can be overridden */
    			'test' => 'boo!',
    		),$config);
    	}
    	
    	public function run() {
    		print $this->main->getNumber(); // echoes 3
    		print $this->config['test']; // echoes 'Hooray!';
    	}
    }
    
    $main = new Main();
    $child = $main->create('Child',array(
    	'test' => 'Hooray!',
    ));
    $child->run();
    



    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.
    I prefer arrays as class vars. Easier to extend, iterate, and use, imo. MODx Revolution uses a $config array, along with a function called getOption.
      shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      Thank you again! This is really helpful!

      Yes, Jason, you’re correct: I’m not extending the MODx class. What I want to do is reference the $modx object to take advantage of its functions. Right now I have a module that instantiates my object:

      // Pasted inside into the MODx module code:
      include('/path/to/MyObj.php');
      $args=array('of' => 'stuff');
      $my_obj = new MyObj($args);


      And then inside the included MyObj file I have something like this:
      global $modx;
      $this->MY_modx = &$modx;


      That works, but I get the feeling this is really low-brow. Is there a better way to do this?

      How is it possible to EXTEND the modx class at runtime ? One of the thing that doesn’t fit in my brain very well is how you sort-of leap-frog the parent and initialize the CHILD: e.g.

      class Parent {
      // stuff
      }
      class Child extends Parent
      {
      // more stuff, function overrides, etc.
      }
      
      $extended = new Child();
      


      How would you possibly reference the function overrides etc. in the Child class if you were dealing with an object that was instantiated by
      $normal = new Parent();


      In layman’s terms, how could have a MODx module or snippet override an ALREADY-declared MODx function?