Object-oriented PHP

Source: Internet
Author: User

Instances of the class (including inheritance)
<?PHP//Parent Class  classAnimal { Public $name;  Public $age; //constructor, which is automatically invoked when an instance is generated using the new operator    function__construct ($name,$age) {      $this->name =$name; $this->age =$age; }     Public functionShow () {Echo $this->name. ‘ ‘ .$this->age. ' <br/> ' ; }    //Destructors , which are automatically called when objects are destroyed    function__destruct () {Echo' Object destroyed '. ' <br/> '; }  }  //sub-class  classDogextendsAnimal { Public $legs; function__construct ($name,$age,$legs) {      //calling a method of the parent classParent::__construct ($name,$age); $this->legs =$legs; }     Public functionShowlegnumber () {Echo $this->legs. ' <br/> '; }  }  $myDog=NewDog (' Candy ', 10, 4); $myDog-Show (); $myDog-Showlegnumber (); $myDog=NULL; $hisDog=NewDog (' Sally ', 5, 4); $hisDog-Show (); ?>

When executing the object method, PHP will automatically define a special variable of $this (like this one in JS), which represents a reference to the object itself, by using the $this variable and the '-a ' symbol to refer to all the properties and methods of the object itself (this is similar to JS).

A destructor is a function named __destruct () that is called when an object is logged off. Typically, PHP automatically frees the resources that the object occupies at the end of all requests, so the destructor is not so important. However, in some cases it is useful, such as releasing a specified resource or logging information. In the example above, even if the $hisdog object is not assigned null, its destructor will be called automatically at the end of the script execution.

Class inheritance requires the use of the keyword extends, usually used in PHP to represent the parent class, commonly used to access the methods and properties of the parent class, using self to represent subclasses, often used to access constants, static variables, and methods in the current class.

Constants and static properties of classes, methods
    • Constants of the class

In PHP, there is a global constant, defined using the Define keyword. In PHP5, you can also define constants in a class. A constant of a class does not belong to an instance of any class, it belongs to the class itself, and cannot be modified. Defining a class's constants is simple, and you only need to define them with the Const keyword. There are two cases of a reference to a constant of a class: one is a reference inside a class, and the other is a reference outside the class. Whenever you make a reference, you need to use the scope resolution operator (::).

<?PHPclassColor {ConstRed = ' Red '; ConstBlue = ' Blue '; ConstWhite = ' White '; functionGetwhite () {//Internal References      returnSelf::White ; }  }   //External References  EchoColor::RED; $obj=NewColor (); Echo $obj-getwhite ();?>
    • Static Properties, Methods

Each instance of a class has copies of all its properties and methods, but the class can also define static properties and static methods. Static properties (methods), like constants of a class, belong to the class itself and are defined by the keyword static. Access is the same as the constant for the class.

<?PHPclassMyClass {Static $num= 0; function__construct () { self::$num++; }    //count the number of instances of this class    Static functionPrintnum () {EchoSelf::$num. ' <br/> '; }} MyClass::Printnum (); $a=NewMyClass (); MyClass::Printnum (); $b=NewMyClass (); MyClass::Printnum (); $c=NewMyClass (); MyClass::printnum ();?>
Access modifiers

The most important thing to do when developing with object-oriented programming patterns is the access protection of encapsulation and object properties, which is why access modifiers are created. Generally, access protection is implemented with 3 keywords for class properties and methods: public, protected, and private. During development, access to properties or methods needs to be established, and in order to be backward-compatible, the definition of a method without specifying access rights is automatically set to public (if the definition attribute is specified will cause an error).

Class properties and methods that are specified as public are common properties and methods that can be accessed within any scope. can be accessed outside the object by using the object name, the property name (method name), which can be accessed inside the object using the $this-> property name (method name). If a class inherits from this class, the Class property or method can be accessed in the same way, either externally or internally, by the object of the class.

Class properties and methods specified as protected can only be used inside an object, that is, accessed using the $this-> (Protected) property name (variable name). If a class inherits from this class, the protected class properties and methods can also be accessed as above in the methods of the object of the class.

Class properties and methods that are specified as private are similar to protected, can only be accessed inside an object, and cannot be accessed in an instance object of an inherited class, because private properties and methods are not visible in the inherited class.

Summary: The public modifier definition can be accessed from anywhere to class properties and methods; The protected modifier definition can be accessed only within the class and within that class subclass (inheritance); the private modifier definition can be accessed only within the class.

Magic method

In addition to providing static methods in PHP, there are several useful magic methods available. The Magic method here includes the preceding constructor __construct (), the destructor __destruct (), and the string conversion function __tostring () and the Clone function __clone ().

    • String conversion function __tostring ()

When an object is output with Echo or print, the __tostring () method is called automatically (not as good as Var_dump ())

<?PHPclassChild { Public $name;  Public $age; function__construct ($name,$age) {      $this->name =$name; $this->age =$age; }    function__tostring () {return $this->name. ‘ ‘ .$this->age. ' <br/> '; }  }  $myChild=NewChild (' Hanzichi ', 10); Print $myChild; Echo $myChild; Var_dump($myChild);?>
    • Clone function __clone ()

In practice, in addition to using the New keyword to create an object, you can also use the Clone keyword to implement an object clone, and the cloned object will have all the properties of the original object.

<?PHPclassChild { Public $name;  Public $age; function__construct ($name,$age) {      $this->name =$name; $this->age =$age; }    functionShow () {Echo $this->name. ‘ ‘ .$this->age. ' <br/> '; }  }  $myChild=NewChild (' Hanzichi ', 10); $hisChild=Clone $myChild; $hisChild-Show (); Hanzichi Ten?>

Sometimes we don't need to clone exactly the same object, we can define the __clone method in the class, and it will be called automatically when cloning.

<?PHPclassChild { Public $name;  Public $age; function__construct ($name,$age) {      $this->name =$name; $this->age =$age; }    function__clone () {$this->name = ' Candy '; }        functionShow () {Echo $this->name. ‘ ‘ .$this->age. ' <br/> '; }  }  $myChild=NewChild (' Hanzichi ', 10); $hisChild=Clone $myChild; $hisChild-Show (); Candy Ten?>

Object-oriented PHP

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.