PHP Object-oriented explanation

Source: Internet
Author: User
"Share" PHP object-oriented detailed

Object-oriented object concept is the core of object-oriented technology. The things we face in the show world are objects, such as computers, televisions, bicycles, etc. In object-oriented program design, object is a whole that consists of information and the description of information processing, and it is an abstraction to the real world.

Main three attributes of an object
Object behavior: You can apply those actions to objects, turn on the lights, turn off the lights is the behavior.
The shape of the object: When applying those methods is how the object responds, color, size, shape.
Object representation: The representation of an object is equivalent to an identity card, specifically distinguishing between the same behavior and the state of what is different.

Object-oriented model

Object-oriented concepts:
OOP (object-oriented programming) it could be that its code is simpler, easier to maintain and more robust.

What is a class:
A class is a collection of objects that have the same properties and services for example, people, books, ships, cars belong to the class, he made a uniform abstract description of the objects belonging to the class, in the programming language of the class is a separate program, it should have a class name including the description of the property and the service two parts.
What is an object:
An object is an entity that describes an objective event in the system, which is a basic unit of the system. * Data and code are bundled in an entity *, an object consists of a set of properties and a set of behaviors that manipulate this set of properties.
From an abstract point of view, an object is an abstraction of a problem domain or some thing in the implementation domain. He reflects the information that the thing holds in the system and the role it plays: It is a set of attributes and a wrapper that has permission to manipulate these properties. The objective world is made up of the connections between objects and objects.
Class and object relationships:
The relationship between a class and an object is like the relationship between a mold and a casting, and the result of the strength of the class is the object, and the abstract of the object is the class, which describes a set of objects that have the same attributes (attributes) and the same behavior.

Classes and properties and methods
The class syntax format is defined in PHP:
class classname [optional attribute]{
Public $property [=value]; ...//Declare a common identity with a, and then give a variable variable can also assign a value
function functionname (args) {//member functions in Methods of class
Code} ...
Method of the Class (member function)
}

Build object (Instantiation of Class): $ object Name =new classname ();
Using the properties of an object
In a class, you can access a special pointer $this when you set or access the variable by an action in the class, use $this->name to refer to it.
Generation of objects
After defining a class with a new to declare, because of the encapsulation characteristics of object data, object can not be accessed directly by the main program block by the object to invoke the properties and behavior functions defined in the class, and indirectly to achieve the purpose of accessing the data in the control class.
Relationship of objects and classes
Relationship of objects and classes:
objects are actually present and occupy dynamic resources.
A class is the blueprint for an object and may occupy a static resource.
Object properties Occupy dynamic resources
A class (static) property is actually a "global variable" on a class namespace
Performance considerations:
Each object consumes data space alone
Increased call hierarchy may consume execution time
Parameter form and delivery mode of the method
The parameters of a method can be basic data types, arrays, and class objects.
Basic data type: Value parameter delivery
Array: Value parameter Pass
Class object: Reference delivery
constructor function
Constructors are functions that initialize in a class
Constructors are generated in the same way as other functions except that their names must be __construct ().
Syntax format: function __construct (parameter) {
。。。。。。。。
}
Example:
Class person{
Public $name;
Public $sex;
Public $age;
function __construct ($name, $sex, $age) {
echo "I am a constructor
";
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
Output Result: Initialization
Destructors
The destructor is automatically executed when the object is detached from its scope (for example, the function where the object is already called). The memory should be freed in the destructor before exiting.
Destructors __destruct destructors do not have any parameters
Example:
Class person{
function _ _destruct ()
{echo "Bye bye!";}
}
$a =new person ();

Type of Access
Public common (public modifier) class inside and outside of the class can be accessed
Private (private modifier) can only be accessed within the class
Protected protected (protected member modifier) subclasses can access classes outside of the class that cannot be accessed

Three key features of OOP
Encapsulation, inheritance, polymorphism
Encapsulation: Encapsulation is the combination of the object's properties and behavior into a separate 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; only static properties can be manipulated 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 ();
Printinfo ($a); Output a INFO
Printinfo ($b); Output b INFO
Printinfo ($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; Error
function Start ();
function Stop ();
}
The methods in the interface can be declared as static
Interface a{function A ();}
Interface b{function B ();}
Interface C extends a{function C ();}
Class D implements b,c{
function A () {}
Function B () {}
Function C () {}
}

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