PHP Object-oriented

Source: Internet
Author: User

Similar java,c++,php also have object-oriented, about classes, objects. Inherit, encapsulate ... The concept is similar, so here is a straightforward way to illustrate the use of object-oriented PHP.

I. Declaration of a class

Class person{

Member properties

The member property must be keyword decorated before it has 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; I feel public

Private $age; Private: Embodies encapsulation. cannot be assigned outside the value, get. Interview. Can only be in the internal

Static $sex;

Member Methods

function run () {//default public

Method body

}

Function Eat ($food) {

Method body

}

Private Function sleep () {

Method body

}

Magic methods, which have been defined in advance, but must be declared in the class and defined by the method body

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

The first method that is invoked (once) by the object itself after the object is created. If the declaration is not displayed. The system will generate by default

Only one constructor can be found in PHP (PHP cannot have a duplicate name function). However, the overloaded functionality can be implemented by default parameters

Method body

}

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

The object loses a reference. Self-invocation before being recycled by the garbage collector (the stack is advanced and out of character. The backward first loses the reference)

Method body

}

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

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 gets whether the property is defined, same as __set, call Isset on its own, non-private directly to Isset

Method body

}

Private Function __unset ($name) {///externally has the property of the delete secret member, with __isset, when calling Usset externally, not private directly to unset

Method body

}

}

Two. Instantiation of a class

$me = new Person (); If the class has a constructor with parameters, you can increase the number of references in parentheses

  

Three. Access by members

nonstatic (Static) member

$me->name = "Lee";

$me->eat ("Apple");

Interview among members of the object

$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 overridden methods in subclasses cannot be lower than the parent class's

Six. Common keyword and Rubik's Cube method

In addition to the above keyword and Rubik's Cube method. And also:

Final: Adornment 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. Just read. Interview method with Static

__clone () {//Clone an object once again, and invoke it on its own initiative ($this this copy object. $that original object) $p 2 = Clone $p 1;//clone

Method body

}

__tostring () {//The Output object is invoked on its own initiative. Returns an object that is in string form

Method body

Return

}

__call ($var 1, $var 2) {//Call a method that does not exist in the class when it calls itself (otherwise the error exits the run). $var 1 accepts the method name. $var 2 accepted array of parameters

Method body

}

__sleep () {///serialization is self-invoking, serialization of partial properties

Method body

Return

}

__wakeup () {///crossdress is invoked on its own initiative when it is serialized. Assign a value again for a partial property

Assign value

}

Seven. Other

__autoload ($lei) {//When the class being used is not defined by itself (not declared in class, global function)

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

}

Serialize ($lei); Class serialization (Saves the class as a 26-character string network transmission or continuation,), Unserialize () deserializer

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

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.