PHP Object-oriented

Source: Internet
Author: User

Similar java,c++,php also have object-oriented, about classes, objects, inheritance, encapsulation ... The concept is similar, so here is a direct explanation of PHP object-oriented usage.

I. Declaration of a class

Class person{

Member properties

You must have a keyword modifier before the member property, and you need to have a Var

Member properties do not use resource types and empty types (resource null), and it is best not to assign an initial value when declaring a class.

var $name; Default to Public

Private $age; Private: encapsulation, can not be assigned in the external, access, accessible, only in the internal

Static $sex;

Member Methods

function run () {//default public

Method body

}

Function Eat ($food) {

Method body

}

Private Function sleep () {

Method body

}

Magic method, pre-defined, but must be declared and custom method body in class

__construct ($var 1= ", $var 2 =") {//construction method, do not set to private

Once the object is created, the first method is automatically called by the object (once), and if no declaration is displayed, the system will generate the default

PHP can have only one constructor (cannot have the name function in PHP), but can use the default parameters to implement the overloaded function

Method body

}

function __distruct () {//destructor, cannot take parameters, preferably not private

When an object loses a reference, it is automatically called before it is reclaimed by the garbage collector (stack advanced post-feature, backward first loses the reference)

Method body

}

Private Function __set ($name, $value) {//externally assigns a value to the property of a privately-owned member, automatically called, preferably private

Method body

}

Private Function __get ($name) {///external Gets the property of the member, same as __set

The method body should be returned at last, unfavorable output

}

Private Function __isset ($name) {//External get Private member property is defined, same as __set, called automatically when calling Isset externally, non-private directly to Isset

Method body

}

Private Function __unset ($name) {///externally is deleted by the __isset, and is automatically called when Usset is called externally, not private directly to unset

Method body

}

}

Two. Instantiation of a class

$me = new Person (); If there is a constructor with parameters in the class, you can add the arguments in parentheses

  

Three. Access of Members

nonstatic (Static) member

$me->name = "Lee";

$me->eat ("Apple");

Inter-member access within objects

$this->name = "Me";

Calling methods that are overloaded in the parent class

Parent::__construct ();

class internal static (static), constant (const)

Self::name;

Self::eat (' foot ');

class external static (static), constant (const)

Person::name;

Person::eat (' food ');

Four. Inheritance of classes

Class Student extends person{

Members

}

  

Five. Method overloading

The access control permission for the overridden method in a subclass cannot be lower than the parent class's

Six. Common keywords and Rubik's Cube method

In addition to the above keyword and Rubik's Cube method, there are:

Final: Decorated class, cannot be inherited. Modified method, cannot be overwritten after inheritance. Otherwise the error. Properties cannot be decorated.

Static: Statically member. Modifies member variables, member methods. Unique exists, shared. belongs to the class and does not belong to the object.

Const: In-Class constants. The initial value is assigned when declaring. Read-only. Access Mode with static

__clone () {//Reinitialize when cloning an object, automatic invocation ($this This Copy object, $that original object) $p 2 = Clone $p 1;//clone

Method body

}

__tostring () {//Output object is automatically called when returned object is in string form

Method body

Return

}

__call ($var 1, $var 2) {//called automatically when a method that does not exist in the class is invoked (otherwise an error exits execution), $var 1 accepts the method name, $var 2 accepts the parameter array

Method body

}

__sleep () {//serialization is automatically called, serialization of partial properties

Method body

Return

}

__wakeup () {///crossdress is called automatically when it is serialized, and the partial property is re-assigned.

Assign value

}

Seven. Other

__autoload ($lei) {//automatically called when the class being used is not defined (not declared in the class, global function)

Include (..  $var.); Ucfirst (); full-load change to lowercase;

}

Serialize ($lei); class serialization (Saving a class as a binary string for network transmission or persistence), unserialize () serialization

PHP Object-oriented

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.