Jump to content

method returns unexpected result


johnmerlino

Recommended Posts

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
?>

Link to comment
https://forums.phpfreaks.com/topic/235428-method-returns-unexpected-result/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.