PHP object oriented implementation code

  • 2020-03-31 16:43:35
  • OfStack

1. Simple object creation
 
//Class scope final: prevents subclasses from overriding this field
//Method scope abstract: declared in the parent class and implemented in the subclass
//Define the class:
class Employee{ 
//Define fields
private $name; 
protected $title; 
public $wage; 
//constant
const PI = 3.1415926; 
//Static member variable
private static $visitors = 0; 
//Define the constructor
function __construct(){ 
// use Static member variable
self::$visitors++; 
echo "constructor"; 
} 
//Define a destructor
function __destruct(){ 
echo "destruct"; 
} 
//The declarative approach
public function clockIn(){ 
//Use field
echo "Member $this->name"; 
} 
//When accessing a property (such as a private field) that an object does not own, the method or the set method are automatically called if the object's user is s/s s s/s methods
function __set($property,$value){ 
$this->$property = $value; 
} 
function __get($property){ 
return $this->$property; 
} 
} 
//The inherited Manager of the class inherits Employee
class Manager extends Employee { 
function __construct(){ 
//Calls the constructor or method of the parent class
parent::__construct(); 
parent::clockIn(); 
echo "Manager constructor"; 
} 
} 
//Create an object
$employee = new Employee(); 
$employee->wage = 10000; 
// use constant
echo Employee::PI; 
//A method is called
$employee->clockIn(); 
$manager = new Employee(); 
//Instanceof determines whether an object is an instanceof a class, a subclass of a class, or an implementation of a particular interface
if($manager instanceof Employee ) echo "Yes"; 

2. Advanced OO features
(1) object cloning
 
//Object cloning
class ClassA{ 
private $name; 
private $title; 
public function setName($name){ 
$this->name = $name; 
} 
function getName(){ 
return $this->name; 
} 
public function setTitle($title){ 
$this->title = $title; 
} 
public function getTitle(){ 
return $this->title; 
} 
function __clone(){ 
echo " I was cloned ","<br>"; 
} 
} 
$classA = new ClassA(); 
$classA->setName("NameA"); 
$classA->setTitle("TitleA"); 
$classB = clone $classA; 
$classB->setName("NameB"); 
echo $classA->getName(),"<br>",$classA->getTitle(),"<br>"; 
echo $classB->getName(),"<br>",$classB->getTitle(),"<br>"; 
 

(2) interface
 
//interface
interface IPillage{ 
function method(); 
} 
class ClassC extends ClassA implements IPillage { 
function method(){ 
echo "inteface method"; 
} 
} 
$classC = new ClassC(); 
$classC->method(); 
//inteface method 

(3) abstract class
 
//An abstract class is a class that cannot be instantiated and can only be used as a base class inherited by other classes
abstract class BaseClass{ 
protected $name; 
abstract function method(); 
} 
class ChileClass extends BaseClass { 
function method(){ 
echo "method"; 
} 
} 
$child = new ChileClass(); 
$child->method(); 
//output method 

Note:
If you want to create a model that will be adopted by closely related objects, you can use abstract classes. If you want to create functionality that is adopted by some unrelated object, use the interface.
Use interfaces if you must inherit behavior from multiple sources. PHP can inherit multiple interfaces, but it cannot extend multiple abstract classes.
If you know that all classes share a common behavior implementation, use an abstract class and implement the behavior in it. Behavior cannot be implemented in an interface.

Related articles: