PHP Design Patterns .

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; } }
To download source right-click here and "Save As...".


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); }
To download source right-click here and "Save As...".


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;
To download source right-click here and "Save As...".


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


Design Patterns
Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides

PHP 5
The Official PHP web site
Core PHP Programming, 3rd Edition by Leon Atkinson and Zeev Suraski
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by baotou_lu on 2014-07-04 Rate this Comment

Hi, could you tell me which design pattern is an ideal answer at the following quesiton?
Validating form elements, or any situation where multiple algorithms can be implemented via a single interface, is an ideal candidate for the ____________ design pattern.
I have no idea what it should be. Thank you in advance.

Henry

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.