gilley Posted June 21, 2015 Share Posted June 21, 2015 Hello, I think this is pretty basic stuff for you but it is giving headache for me, hope you can help me. So I have this piece of code: <?php class example { //protected $a; //protected $c; public function NumberInput($number){ $this->a = $number; } public function NumberOutput(){ return $this->a; } } class example2 { public function __construct(){ $this->mergingclass = new example; } public function numberInput2($number){ $this->c = $number; } public function NumberOutput2(){ return $this->c; } public function __call($method,$arguments){ echo "called method is $method <br> "; return $this->x = $arguments; } } $b = new example2; $b->NumberInput(7); echo $b->NumberOutput(); ?> And here are things that are confusing me: 1) this code is working the same way when I delete __construct function , or atleast I don't notice the change. I have read manual about construct method but I still don't understand what does it do. 2) it doesn't echo number 7 when I execute it, but instead I get just "Array" written. Why is it storing number 7 in array ?. How can I echo just number 7 ? Thanks a lot in advance Quote Link to comment Share on other sites More sharing options...
maxxd Posted June 21, 2015 Share Posted June 21, 2015 (edited) Example2 doesn't have NumberInput() or NumberOutput() methods, it has numberInput2() and NumberOutput2() methods. In the constructor function, you call and instantiate a new instance of the example() class, but don't actually do anything with it. That's the class that has numberInput() and numberOutput() methods. So, when you call NumberInput() on the example2() object, the magic method __call() is catching that call to a non-existing method. If you look at the description for the __call() magic method, you'll see that the $arguments parameter is an enumerated array, which is the output you're getting. It looks like what you're attempting to experiment with is class inheritance and polymorphism. Consider the following long-winded and inane example: class Example{ /** * @var int An integer */ protected $_number; /** * Because $this->_number is protected, you can't access it directly from outside this class * This method allows the user to set the value of $this->_number * @param int $num Number * @return void * @throws Exception When the submitted value is not an integer */ public function setNumber($num){ if(!is_int($num)){ throw new Exception("That's not a number, you twink!"); } $this->_number = $num; } /** * Again, $this->_number is inaccessible. This returns the value of $this->_number to * the user. * @return int */ public function getNumber(){ return $this->_number; } } class Example2 extends Example{ /** * @var string A greeting */ private $_message; /** * This will be called every time you instantiate a new Example2() object. * It sets the value you pass into the class in the private $_message variable. * @param string $msg A greeting * @return void */ public function __construct($msg){ $this->_message = $msg; } /** * This public method will overwrite the parent class's getNumber() method and return the value * of parent::$_number plus 2. Because I felt like adding 2 to it. This is polymorphism - it serves * the same purpose as the parent method, uses the same implicit API, but returns a different value; * the calling code knows it can call this method and get something back that it can work with, but * it really doesn't care *what* it gets back. * @return int */ public function getNumber(){ return $this->_message." :: ".$parent->_number + 2; } } $class = new Example2('Well hi there! This is a number'); $class->setNumber(42); print("<p>".$class->getNumber()."</p>"); When this is run, it outputs "Well hi there! This is a number :: 44". The call to setNumber() on $class will bubble up to the parent class, where there is a method called setNumber(). Note that the Example2::getNumber() method refers to $parent->_number directly - because $_number is Example is protected, the child classes can do that. If it were private, this would cause an error and the Example2() class would have to use $parent->getNumber() from within it's own getNumber() method. Hopefully that makes some sort of sense and will help put you onto the right track. I'd also recommend checking out PHP Objects, Patterns, and Practice by Matt Zandstra - it's well written, clear, and explains a lot. Highly recommended. Edited June 21, 2015 by maxxd Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.