11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 7 • ADVANCED OOP FEATURESwww.it-ebooks.infofunction __construct($name) {$this->setName($name);}Then you instantiate the CEO class and retrieve the name property:$ceo = new CEO("Dennis");echo $ceo->getName();It will yield the following:My name is DennisHowever, if the child class also has a constructor, that constructor will execute when the child classis instantiated, regardless of whether the parent class also has a constructor. For example, suppose thatin addition to the Employee class containing the previously described constructor, the CEO class containsthis constructor:function __construct() {echo "CEO object created!";}Then you instantiate the CEO class:$ceo = new CEO("Dennis");echo $ceo->getName();This time it will yield the following output because the CEO constructor overrides the Employeeconstructor:CEO object created!My name isWhen it comes time to retrieve the name property, you find that it’s blank because the setName()method, which executes in the Employee constructor, never fires. Of course, you’re probably going towant those parent constructors to also fire. Not to fear because there is a simple solution. Modify the CEOconstructor like so:function __construct($name) {parent::__construct($name);echo "CEO object created!";}Again instantiating the CEO class and executing getName() in the same fashion as before, this timeyou’ll see a different outcome:166

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!