OOP Advanced
Keywords: interface, abstract, final, access, exception, lazy loading.
autoload, reflection.
Subjects:
12.1.
Interface
12.2.
Abstract Class, Method
12.3.
Final Class, Method
12.4.
Lazy Loading
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
150
12.1.
Interface:
12.1.1. Interface Introduction:
- Object interfaces allow you to create code which specifies which methods a class must
implement, without having to define how these methods are handled.
12.1.2. Declaration:
- Interfaces are defined using the interface keyword, in the same way as a standard class, but
without any of the methods having their contents defined.
- All methods declared in an interface must be public, this is the nature of an interface.
- Example:
interface IAnimal { function eat(); function say(); }12.1.3. Implementation:
interface IAnimal { function eat(); function say(); }class Dog implements IAnimal {
function eat(){ echo 'Dog is eating'; } function say(){ echo 'I am a dog'; } }
class Cat implements IAnimal {
function eat(){ echo 'Cat is eating';} function say(){ echo 'I am a cat'; } }
$dog = new Dog(); $cat = new Cat();
$dog->say(); // Display "I am a dog" $cat->say(); // Display "I am a cat"
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
151
12.2.
Abstract Class, Method:
- An abstract class defines the basic skeleton for the class. It contains properties and methods
but some methods are incomplete and is waiting for some other class to extend it through
inheritance so that the derived class provides a full functionality for the incomplete
methods.
- A abstract class cannot be instantiated and it can only be extended. A class prefix with
“abstract” keywords are abstract class.
- If a method is defined as abstract then it cannot be declared as private (it can only be public
or protected).
- Syntax:
abstract class classname
{
//properties and methods
abstract function methodname(); }
class derived extends classname
{
function methodname(){} }
- Example:
abstract class Review {
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
152
abstract function record();
public function normalMethod(){echo 'it is a normal method';} }
class CustomerReview extends Review {
function record(){ echo 'Customer Review record';} }
class EditorialReview extends Review {
function record(){echo 'Editorial Review record';} }
$cusReview = new CustomerReview();
$cusReview->record(); // Displays "Customer Review record" $review = new Review(); // Fatal Error
12.3.
Final Class, Method:
12.3.1. Final Class:
- A final class is a class that cannot be extended. To declare a class as final, you need to prefix
the ‘class’ keyword with ‘final’.
- You should declare a class as final when you think that you implementation of that class
should not change in the derived class. You should do this mainly for Utility classes where
you don’t want the behavior/implementation of your class to change.
- Example:
final class FinalClass {
function hello(){echo 'Final class says hello!';} }
class ChildFinalClass extends FinalClass {
}
12.3.2. Final Method:
- Final method is a method that cannot be overridden. To declare a method as final, you need
to prefix the function name with the ‘final’ keyword
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
153
- You should declare a class method as final when you think that the method you develop
contains necessary functionality to support your application and any modification or
change to the functionality can cause unexpected errors/bugs.
- Example:
class NormalClass {
final function hello(){echo 'Normal class says hello!';} }
class ChildNormalClass extends NormalClass {
function hello(){echo 'testing';} }
12.4.
Lazy Loading:
12.4.1. Introduction:
- Loading of class on-demand.
- When referencing a non-existent class, be it as a type hint, static call, or instantiating an
object, PHP will try to call the __autoload() global function so that the script may be given
an opportunity to load it.
12.4.2. Magic Method __autoload() :
- The magic method __autoload() function is a convenience that allows you to use classes
without having to explicitly write code to include them.
- The magic method __autoload() is not included in your class definition as this is to be called
once in a script. The best place to put the autoload() file is in your configuration file which
is loaded in all your other scripts.
-The __autoload() method is called only once for each new class that needs to be loaded.
Subsequent instantiation of the Customer class object will not call the __autoload() method
again. Therefore, this offers performance improvements in your scripts because, unless the
class is needed - files are not loaded. Therefore, the PHP engine does not have to parse and
compile an unnecessary file.
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
154
function __autoload($class) {
require_once str_replace(" ", "/", $class); }
$obj= new Some_Class();
12.5.
Magic Methods:
12.5.1. Property Overloading:
12.5.1.1. __get() : reading data from inaccessible properties. Example:
Class test {
public $var1 = 'abc'; private $pvar2 = 'hello'; function __get($name) { $privateName = 'p'.$name; if(isset($this->$privateName)) { return $this->$privateName; } else return ''; } }
$myTest = new test();
echo $myTest->var1; // Displays "abc" echo $myTest->pvar2; // Displays nothing echo $myTest->var2; // Displays "hello"
12.5.1.2. __set() : writing data to inaccessible properties. Example:
Class test {
public $var1 = 'abc'; private $pvar2 = 'hello'; function __get($name) {
$privateName = 'p'.$name; if(isset($this->$privateName))
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
155
{ return $this->$privateName; } else return ''; }
function __set($name, $value) {
$privateName = 'p'.$name; if(isset($this->$privateName)) {
//can validate input data here //…
//assign to private property $this->$privateName = $value; }
} }
$myTest = new test();
$myTest->var1 = 'def'; // Assigns $var1 = "def" $myTest->pvar2 = 'world'; // Do nothing
$myTest->var2 = 'world'; // Assigns $pvar2 = "world" echo $myTest->var2; // Displays "world"
12.5.1.3. __isset() : is triggered when calling isset() or empty() on inaccessible properties.
Example:
Class test {
public $var1 = 'abc';
private $p = array('var2'=>'hello'); function __isset($name) { if(isset($this->p[$name])) { return true; } else return false; } }
$myTest = new test();
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
156
var_dump(isset($myTest->pvar2)); // Displays "bool(false)" var_dump(isset($myTest->var2)); // Displays "bool(true)"
12.5.1.4. __unset() : is triggered when calling unset() on inaccessible properties. Example:
Class test {
public $var1 = 'abc';
private $p = array('var2'=>'hello'); function __unset($name) { if(isset($this->p[$name])) { unset($this->p[$name]); } } }
$myTest = new test(); unset($myTest->var1); unset($myTest->var2);
12.5.2. Method Overloading:
12.5.2.1. __call() : is triggered when invoking inaccessible methods in an object context.
Example:
Class test {
public function __call($name, $arguments) {
echo "Calling $name(".implode(', ', $arguments).")\n"; }
}
$myTest = new test();
$myTest->unknowfunction(1,2);
// Displays "Calling unknowfunction(1, 2)"
12.5.2.2. __callStatic() : is triggered when invoking inaccessible methods in a static
context. Note: PHP Version >= 5.3. Example:
Class test {
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
157
{
echo "Calling Static method test::$name(".implode(', ', $arguments).")\n";
} }
test::unknowfunction(1,2);
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING