Generally speaking, learning PHP requires understanding the following features:
Object cloning. One of the major improvements to the OOP model in PHP5 is to treat all objects as references instead of values. But if all objects are treated as references, how do you create a copy of an object? The answer is by cloning the object.
<?php class Corporate_Drone{ private $employeeid; private $tiecolor; function setEmployeeID($employeeid) { $this->employeeid = $employeeid; } function getEmployeeID() { return $this->employeeid; } function setTiecolor($tiecolor) { $this->tiecolor = $tiecolor; } function getTiecolor() { return $this->tiecolor; } } $drone1 = new Corporate_Drone(); $drone1->setEmployeeID("12345"); $drone1->setTiecolor("red"); $drone2 = clone $drone1; $drone2->setEmployeeID("67890"); printf("drone1 employeeID:%d <br />",$drone1->getEmployeeID()); printf("drone1 tie color:%s <br />",$drone1->getTiecolor()); printf("drone2 employeeID:%d <br />",$drone2->getEmployeeID()); printf("drone2 tie color:%s <br />",$drone2->getTiecolor()); ?>
Inherited. As mentioned earlier, building a class hierarchy through inheritance is a key concept of OOP.
class Employee { ... } class Executive extends Employee{ ... } class CEO extends Executive{ ... }
Interface. An interface is a collection of unimplemented method definitions and constants, which is equivalent to a class blueprint. The interface only defines what the class can do, but does not involve implementation details. This chapter introduces PHP5's support for interfaces and provides some examples that demonstrate this powerful OOP feature.
<?php interface IPillage { // CONST 1; function emptyBankAccount(); function burnDocuments(); } class Employee { } class Excutive extends Employee implements IPillage { private $totalStockOptions; function emptyBankAccount() { echo "Call CFO and ask to transfer funds to Swiss bank account"; } function burnDocuments() { echo "Torch the office suite."; } } class test { function testIP(IPillage $ib) { echo $ib->emptyBankAccount(); } } $excutive = new Excutive(); $test = new test(); echo $test->testIP($excutive); ?>
Abstract class. Abstract classes are essentially classes that cannot be instantiated. An abstract class will be inherited by an instantiable class, which is called a concrete class. An abstract class can be fully implemented, partially implemented, or not implemented at all.
abstract class Class_name { //insert attribute definitions here //insert method definitions here }
Namespace. Namespaces can divide various libraries and classes according to context, helping you manage your code base more effectively.
<?php namespace Library; class Clean { function printClean() { echo "Clean..."; } } ?> <?php include "test.php"; $clean = new \Library\Clean(); $clean->printClean(); ?>
If you have used other object-oriented languages, you may be wondering why the above features do not include some OOP features that are familiar in other languages? The reason is simple, PHP does not support these features. To save you from confusion, here is a list of advanced OOP features that PHP does not support.