PHP object-oriented implementation code

Source: Internet
Author: User
If you want to create a model, this model will be used by closely related objects, you can use abstract classes. 1. simple object creation

The code is as follows:


// Class scope final: This field will be blocked from being overwritten by subclass
// The scope of the method abstract: declared in the parent class, implemented in the subclass
// Definition 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 variables
Self: $ visitors ++;
Echo "constructor ";
}
// Define the destructor
Function _ destruct (){
Echo "destruct ";
}
// Declaration method
Public function clockIn (){
// Use fields
Echo "Member $ this-> name ";
}
// When accessing a property that an object does not possess (such as a private field), if the object User _ get and _ set methods, the _ get method or _ set method is automatically called.
Function _ set ($ property, $ value ){
$ This-> $ property = $ value;
}
Function _ get ($ property ){
Return $ this-> $ property;
}
}
// Class inheritance Manager inherits Employee
Class Manager extends Employee {
Function _ construct (){
// Call 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 constants
Echo Employee: PI;
// Call method
$ Employee-> clockIn ();
$ Manager = new Employee ();
// Instanceof determines whether an object is an instance of a class or a subclass of a class, or whether a specific interface is implemented
If ($ manager instanceof Employee) echo "Yes ";


2. advanced OO features
(1) object cloning

The code is as follows:


// 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 have been cloned ","
";
}
}
$ ClassA = new ClassA ();
$ ClassA-> setName ("NameA ");
$ ClassA-> setTitle ("TitleA ");
$ ClassB = clone $ classA;
$ ClassB-> setName ("NameB ");
Echo $ classA-> getName (),"
", $ ClassA-> getTitle (),"
";
Echo $ classB-> getName (),"
", $ ClassB-> getTitle (),"
";
/* Output
I have been cloned.
NameA
TitleA
NameB
TitleA
*/


(2) interface

The code is as follows:


// 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

The code is as follows:


// Abstract class, which cannot be instantiated and can only be used as the 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, this model will be used by closely related objects, you can use abstract classes. If you want to create functions used by irrelevant objects, use interfaces.
If the behavior must be inherited from multiple sources, the interface is used. Php can inherit multiple interfaces, but cannot extend multiple abstract classes.
If you know that all classes share a public behavior implementation, use an abstract class and implement this behavior in it. Actions cannot be implemented in interfaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.