Re: Classes and parents.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dmitry wrote:
Greetings.

If i run this code (php5):
------------------------------------------
class a {
 function say() { echo "A"; }
 function run() { $this->say(); }
}
class b extends a {
 function say() { echo "B"; }
 function run() { parent::run(); }
}

$obj = new b;
$obj->run();
---------------------------------------

I will get "B", but how i may get "A"?


php -r '

class a {
 function __construct($speak = false) { $this->run($speak); }
 protected function say($speak) { if($speak) { echo "A"; } }
 public function run($speak = true) { $this->say($speak); }
}

class b extends a
{
private $base;
function __construct($speak = false)
{
	$this->base = new A($speak);
	if ($speak) {
		$this->run();
	} else {
		$this->base->run();
	}
}
protected function say($speak)
{
	if($speak) {
		echo "B";
	}else {
		parent::say(!$speak);
	}
}
public function run($speak = true)
{
	$this->say($speak);
}
}

$obj = new b;$obj->run(false); echo "or try out reflection?\n"'
AA
or try out reflection?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux