note 101548 modified in keyword.extends by danbrown

From: Date: Thu, 23 Dec 2010 19:41:36 +0000
Subject: note 101548 modified in keyword.extends by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-175179@lists.php.net to get a copy of this message
If you have a class that extends an other and both classes have a function with the same name then visibility to both classes should be the same, else you will have a fatal error. An other interested part in the example bellow is that if visibility of the showName method is private then $this->name() will execute the showName method on class test. If it is public or protected it will execute method showName on class extendTest. <?php class test { public function __construct() { } public function name() { $this->xname('John'); } private function showName($name) { echo 'my name in test is '.$name; } } class extendTest extends test { public function __construct() { parent::__construct(); } private function showName($name) { echo 'my name in extendTest is '.$name; } } $test = new extendTest(); $test->name(); ?> result: my name in test is John If we change visibility of the showName method to public or protected then the result of the above will be: my name in extendTest is John --was-- If you have a class that extends an other and both classes have a function with the same name then visibility to both classes should be the same, else you will have a fatal error. An other interested part in the example bellow is that if visibility of the showName method is private then $this->name() will execute the showName method on class test. If it is public or protected it will execute method showName on class extendTest. class test { public function __construct() { } public function name() { $this->xname('John'); } private function showName($name) { echo 'my name in test is '.$name; } } class extendTest extends test { public function __construct() { parent::__construct(); } private function showName($name) { echo 'my name in extendTest is '.$name; } } $test = new extendTest(); $test->name(); result: my name in test is John If we change visibility of the showName method to public or protected then the result of the above will be: my name in extendTest is John http://php.net/manual/en/keyword.extends.php

« previous php.notes (#175179) next »