What are the features of PHP oop? Introduction to three features of PHP oop ideas

Source: Internet
Author: User
Tags php oop
PHP oop idea of three major characteristics are: encapsulation, inheritance and polymorphism, here, I will be specific and everyone to say a bit about the idea of PHP opp understanding, then, we will come to a concrete look at the three characteristics of the OPP idea and the PHP opp idea.
Encapsulation of

Encapsulation is the combination of the object's attributes and behavior into a single unit.
Encapsulating a class requires two steps the first step is to privatize a class the second step is to use set and get to make read assignment operations
His advantage is: hide the implementation details of the class, you can easily add logic control, restrict the unreasonable operation of the attribute, easy to modify the maintainability of the enhanced code.

__get and __set
It is generally said that the private words of the class more in line with the logic of reality.
Two functions are predefined to obtain and apply the value of the operation.
__get gets a value that is usually the value of a field
__set setting value is usually the value of the field
When __call calls a method that does not exist in an object, an error call () is generated to handle the situation.

Static Properties and methods
The static keyword to declare the Statics method
Static variables that generate a static variable inside a class are capable of being shared by all classes of power. That is, static members are placed in the "Initialize static segment", which is placed when the class is loaded for the first time, allowing each object in the heap to be shared
How to use: self::$ static property, Self:: Static method

static function P () {echo self:: $country, echo Self::P i;//Access constant//echo $this->name; can only manipulate static properties in static methods//self::p ();}

External calls: Class:: $ static property, class:: Static method

Const keyword: used to generate constant constants is unique and cannot be changed by a formula constant to uppercase
Const CONSTANT = ' CONSTANT value '; Generate a constant
echo self::constant;//class internal access
echo classname::constant;//class external access

Inheritance of
The objects of Class B have all the properties and behaviors of Class A, which is called B inheritance to Class A.
If a class inherits properties and services from multiple classes, this is called multi-inheritance, usually we become the subclass of the inheriting class as the parent class, only single inheritance in PHP, but a parent class can be inherited by more than one class, but only one parent class, but a subclass can have a child class, but the definition of the class is reduced by inheritance.
Extende declaring an inheritance relationship
Syntax format: Class B Extends A This example indicates that B inherits a
The outer access of the class is valid for the child class
Properties and methods for subclasses and parent classes
The subclass inherits all the contents of the parent class, but the private part of the parent class cannot be accessed directly
The newly added properties and methods in the subclass are extensions to the parent class
A property defined in a subclass with the same name as the parent class is an override of the parent class property, and a method of the same name overrides the parent class method.

Overridden methods
In a subclass, use parent to access overridden properties and methods in the parent class
Parent::__construce ();
Parent:: $name;
Parent::fun ();

Overwrite parent class's original property
Clone object Syntax format $c=clone $p; Object of $c $p output echo $c->name;

Object comparison
= = = Two comparison operators.
= = Compares the contents of two objects.
= = = is the handle to the comparison object, which is the reference address.

The instanceof operator is used to detect whether an object's strength belongs to a class of type belonging to return true that does not belong to return false
__clone () If you want to change the contents of the original object after cloning, you need to rewrite the original properties and Methods in __clone ()

function __clone () {$this->name= "I am a clone";}

Final indicates that a class is the final version, which means that it cannot be called in a quilt class

Polymorphism

Polymorphism refers to a property or behavior defined in a parent class that, after the quilt class inherits, can have different data types or behave differently. This makes the same attribute or behavior have different semantics in the parent class and its subclasses.
This means that the same method differs from the result of executing in a subclass with a parent class.

Class A {function info () {echo "A info";}} Class B extends A {function info () {echo "B info";}} Class C extends A {function info () {echo "C info";}} function Printinfo ($obj) {function printinfo (a $obj) {if ($obj instanceof A) $obj->info (); $obj->info ();}} $a =new A (); $b =new B (); $c =new C ();p rintinfo ($a); Output a infoprintinfo ($b); Output b infoprintinfo ($c); Output C INFO

Abstract methods and abstract classes

Abstract methods are used as sub-categories.

Abstract class Person{public $name; abstract function GetInfo ();}

Abstract classes cannot be words of strength, an abstract class must have an abstract method. However, dynamic functions can be defined in abstract classes.
Interface
When a class inherits an interface, it overwrites all methods of the interface, the interface can only declare constants, the method of the interface must be defined as common, otherwise it cannot be inherited, and the interface can inherit from multiple interfaces
Grammar:

Interface Pci{const type= "PCI";//public $name; errorfunction start (); function stop ();} The method in the interface can be declared as Staticinterface a{function A ();} Interface b{function B ();} Interface C extends a{function C ();} Class D implements B,c{function a () {}function B () {}function C () {}}
Related Article

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.