johnmerlino Posted May 3, 2011 Share Posted May 3, 2011 Why does this return null rather than 'john must write the book'? <? abstract class User { protected $name; protected $role; function __construct($name,$role){ $this->name = $name; $this->role = $role; } abstract function run_errand(); } class Author extends User { public function run_errand(){ echo $this->name . "must write the book"; } } class Editor extends User { public function run_errand(){ echo $this->name . "must edit the book"; } } class Boss { private $users = array(); public function review(User $user){ $this->user[] = $user; } public function delegate(){ if(count($this->users)){ foreach($this->users as $user){ if($user->role == "author"){ echo $user->run_errand(); } } } } } //client code $boss = new Boss(); $boss->review(new Author('John','author')); var_dump($boss->delegate()); //returns null ?> Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/ Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 Because delegate() isn't returning anything. If you're wondering why you don't get any output it's because you use "user" in one place and "users" in the others. Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/#findComment-1209934 Share on other sites More sharing options...
johnmerlino Posted May 3, 2011 Author Share Posted May 3, 2011 user is only a local variable in the foreach loop that refers to users, because $this->users is an array of users, so we iterate through them one at a time. Sure that's the issue? Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/#findComment-1210001 Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 Er, not that one. class Boss { private $USERS = array(); public function review(User $user){ $this->USER[] = $user; } That one. (Too bad I can't put highlighting in code tags...) Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/#findComment-1210030 Share on other sites More sharing options...
johnmerlino Posted May 3, 2011 Author Share Posted May 3, 2011 Thanks for response. Why did PHP not thrown an exception error "undefined variable $user on line such and such". The backtrace should have caught that one. Or can you create properties on the fly in PHP? If so, that sucks because that will happen a lot. Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/#findComment-1210107 Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 Or can you create properties on the fly in PHP? Yup. Quote Link to comment https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/#findComment-1210117 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.