PHP Object-oriented implementation Code _php tutorial

Source: Internet
Author: User
1, Simple object creation
Copy CodeThe code is as follows:
Class Scope final: Blocks subclasses from overriding this field
Method Scope Abstract: Declared in a parent class, implemented in a subclass
To define a class:
Class employee{
Defining fields
Private $name;
protected $title;
Public $wage;
Constant
Const PI = 3.1415926;
Static member variables
private static $visitors = 0;
Defining constructors
function __construct () {
Using static member variables
Self:: $visitors + +;
echo "Constructor";
}
Defining destructors
function __destruct () {
echo "Destruct";
}
Declaring methods
Public Function Clockin () {
Using fields
echo "Member $this->name";
}
When accessing a property that an object does not own (such as a private field), the __get method or __set method is called automatically if the object user __get and __set methods
function __set ($property, $value) {
$this $property = $value;
}
function __get ($property) {
return $this $property;
}
}
Inheriting manager of class inherits employee
Class Manager extends Employee {
function __construct () {
Call the constructor or method of the parent class
Parent::__construct ();
Parent::clockin ();
echo "Manager constructor";
}
}
Creating objects
$employee = new Employee ();
$employee->wage = 10000;
Using constants
echo Employee::P i;
Calling methods
$employee->clockin ();
$manager = new Employee ();
Instanceof Determines whether an object is an instance of a class, a subclass of a class, or implements a specific interface
if ($manager instanceof Employee) echo "Yes";

2, Advanced OO Features
(1) Object cloning
Copy CodeThe 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 was 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 was cloned.
Namea
Titlea
Nameb
Titlea
*/

(2) interface
Copy CodeThe 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
Copy CodeThe code is as follows:
Abstract class, which is a class that cannot be instantiated, can only be inherited as a base class for 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 are creating a model, this model will be used by some closely related objects, and you can use abstract classes. Use an interface if you want to create features that are used by some unrelated objects.
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 common behavior implementation, use an abstract class and implement that behavior in it. The behavior cannot be implemented in the interface.

http://www.bkjia.com/PHPjc/320862.html www.bkjia.com true http://www.bkjia.com/PHPjc/320862.html techarticle 1, Simple object creation copy code code is as follows://scope of the class final: blocks subclasses from overriding this field//method scope Abstract: Declared in the parent class, implemented in the subclass/...

  • 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.