PHP Design Patterns PHP OO Abstract Class Basics
PHP Abstract Class Basics
Here is an example of creating a very simple abstract class called OOPHPAbstractClass, and OOPHPClassToExtendAnAbstract which extends it.
Note that to successfully extend OOPHPAbstractClass, OOPHPClassToExtendAnAbstract must have getName() and setName functions().
OOPHPClassToExtendAnAbstract.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
//this class "extends" OOPHPAbstractClass
include_once('OOPHPAbstractClass.php'); class OOPHPClassToExtendAnAbstract extends OOPHPAbstractClass {
private $instanceName; //OOPHPAbstractClass has the abstract function getName, // so we must implement it here. public function getName() { return $this->instanceName; } //OOPHPAbstractClass has the abstract function setName, // so we must implement it here. public function setName($nameIn) { $this->instanceName = $nameIn; } }
OOPHPAbstractClass.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
//OOPHPAbstractClass - a simple OO PHP Abstract Class // this defines two functions, getName() and setName($nameIn) // which any class extending this must have abstract class OOPHPAbstractClass { abstract public function getName();
abstract public function setName($nameIn); }
testOOPHPAbstract.php
//copyright Lawrence Truett and FluffyCat.com 2007, all rights reserved
include_once('OOPHPClassToExtendAnAbstract.php');
define('BR', '<'.'BR'.'>');
echo 'BEGIN TESTING PHP ABSTRACT CLASSES'.BR; echo BR; echo 'test 1 - create a class which extends an abstract'.BR; $classOne = new OOPHPClassToExtendAnAbstract(); echo BR; $classOne->setName("Harold"); echo $classOne->getName(); echo BR.BR;
echo 'END TESTING PHP ABSTRACT CLASSES'.BR;
output of testOOPHPAbstract.php
BEGIN TESTING PHP ABSTRACT CLASSES
test 1 - create a class which extends an abstract
Harold
END TESTING PHP ABSTRACT CLASSES
References
| Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate. |
| Comment by baotou_lu on 2009-06-25 Rate this Comment |
Hi, could you tell me which design pattern is an ideal answer at the following quesiton?
|
| Comment by baotou_lu on 2009-06-24 Rate this Comment |
Hi, your articles are amazing and they are very useful for me to understand the design pattern section in ZCE exam. I viewed all patterns you wrote. I have a small question, maybe it is silly. Can I claim the child class is also an abstract class if it be extended from another abstract class or interfaces without implementing all abstract functions declared in its parent's class or interfaces? In addition, could I ask you some questions about design pattern in the mock test? I am not sure which answer is correct. |
| Sign in to comment on PHP Design Patterns PHP OO Abstract Class Basics. |