PHP Common Keywords and Magic methods in object-oriented explanation

Source: Internet
Author: User
Tags autoload object serialization
1.construct ()

Instantiating an object is called automatically. Call construct when construct and a function that has the class name as the function name are present, and the other is called without a back.

The function with the class name as the function name is the old version of the constructor.

2.destruct ()

When an object is deleted or an object operation ends, it is called.

3.call ()

object to invoke a method. If the method does not exist, call this method

4.get ()

Reads an object property, if the object property is private, it is called

5.set ()

When assigning a value to an object property, it is called if the property is private.

6.toString ()

It is called when an object is printed.

7.clone ()

Called when cloning an object, such as: $a =new test (); $a 1=clone $a;

8.sleep ()

Serialize was called before, if the object is larger, want to cut something in the serialization can use it.

9.wakeup ()

Unserialize is called to do some initialization of the object.

10.isset ()

Detects whether a property of an object exists and is called when the detected property is private.

11.unset ()

Deleting an object property is called if the deleted object property is private.

12.set_state ()

Called when the Var_export is called. Use the return value of Set_state as the return value of Var_export.

13.autoload ()

When an object is instantiated, the method is dropped if the corresponding class does not exist.

The following small series for everyone to bring an elaboration of PHP object-oriented Common keywords and magic method. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

commonly used keywords in PHP object-oriented

Final

1.final cannot modify member properties (constants in a class are not used with this keyword)

2.final can only modify classes and methods

Role:

A class that uses final adornments cannot inherit from a quilt class

You cannot override a quilt class by using the final decoration method

Used to restrict the class from being inherited, the method is not overwritten, and the final

<?php//final modified classes cannot be inherited by the final class person{  var $name;  var $age;  var $sex;  function construct ($name, $age, $sex) {    $this->name= $name;    $this->age= $age;    $this->sex= $sex;  }  function fun1 () {    echo $this->name;  }} Student class inherits class with final modified person class, so error class Student extends person{} $stu =new Student ("Zs", "Nan"); $stu->fun1 ();? >

static (still keyword)

1. Use static to decorate member properties and member methods, not to decorate classes

2. Static decorated member properties that can be shared by all objects of the same class

3. Static data is present in the in-memory data segment (initializes the static segment)

4. Static data is allocated to memory when the class is first loaded, and is fetched directly from the data segment when it is used for the class at a later time

5. What is a class being loaded? As long as the class is used in the program (with this class name appearing)

6. Static method (Static modified method), cannot access non-static member (can access the member in non-static method)

Because non-static members must be accessed using objects, access to internal members is $this, and static methods do not use object invocation, there is no object, $this can not represent objects, non-static members must also use the object

If you determine that a method does not use a non-static member, you can declare the method to be a static method

Note: Static members are accessed using the class name, and do not create objects without object access

Class Name:: Static member

If you use static members in your class, you can use self to represent this class

Const

1. It can only modify member properties

2. Declaring a constant attribute in a class uses the const

3. Access mode is the same as static statically member property (class name is used outside the class:: Constant used inside the class self:: constant)

4. Constants must be given initial value at the time of declaration

<?php//defines a class of "people" class person{  protected $name;  protected $age;  protected $sex;  Static $country = "China";  Declares a constant  const run= "Go";  Constructor method  function construct ($name, $age, $sex) {    $this->name= $name;    $this->age= $age;    $this->sex= $sex;  }  function Getcountry () {    //If you use static members in a class, you can use self to represent this class    return self: $country;  }  function say () {    echo ' my name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";  }  protected function Eat () {    echo "Eat! <br> ";  }  function run () {    //use constant inside the class self:: Constant    echo self::run. " <br> ";  }  Declares a static method of the Statics  function hello () {    echo "Hello <br>";  }}

Common magic methods in object-oriented PHP

Call ()

Function: When calling a method that does not exist in the object, a system error occurs, and then the program exits.

When to invoke automatically: it will be called automatically when a method that does not exist in an object is called

Handle some non-existent error calls

This method requires two parameters

<?php//defines a class of "people" class person{protected $name;  protected $age;  protected $sex;  Static $country = "China";  Declares a constant const run= "go";    Constructor method function construct ($name, $age, $sex) {$this->name= $name;    $this->age= $age;  $this->sex= $sex;  } function Getcountry () {//If you use static members in a class, you can use self to represent this class return self: $country; } function say () {echo ' my name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}.  <br> "; } protected function Eat () {echo "Eat!  <br> "; } function run () {//Use constant inside the class self:: Constant echo Self::run. "  <br> ";    }//Handle some non-existent error calls//will invoke function call automatically when calling a method that does not exist in an object ($methodName, $args) {///$methodName Call the method name of the non-existent method $args parameter    echo "You call the method {$methodName} (parameter:";    Print_r ($args);  echo ") does not exist <br>";  }//Declare static method of the Statics function hello () {echo "Hello <br>"; }} $p =new person ("Zhang San", 20, "female"), $p->test (10,20,30); $p->demo ("AA", "BB"); $p->say ();? 

ToString ()

The quickest way to quickly get a string representation by automatically invoking the direct output object reference

<?php//defines a class of "people" class person{  protected $name;  protected $age;  protected $sex;  Static $country = "China";  Declares a constant  const run= "Go";  Constructor method  function construct ($name, $age, $sex) {    $this->name= $name;    $this->age= $age;    $this->sex= $sex;  }  function say () {    echo ' my name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";  }  function toString () {    return self:: $country. " <br>{$this->name}<br>{$this->age}<br>{$this->sex}<br> ". Self::run;  } $p =new person ("Zhang San", 21, "female"), Echo $p; >

Clone ()

Cloning objects using clone () processing

Original (original object)

Replica (the copied object)

Clone () is the method that is called automatically when cloning an object

As soon as an object is created, there is an initialization action, similar to the constuct function of the constructor method.

The $this keyword in the Clone () method represents the object of the replica, $that represents the original object

<?php//defines a class of "people" class person{  var $name;  protected $age;  protected $sex;  Static $country = "China";  Declares a constant  const run= "Go";  Constructor method  function construct ($name, $age, $sex) {    $this->name= $name;    $this->age= $age;    $this->sex= $sex;  }  function say () {    echo ' my name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";  }  function Clone () {    $this->name= "Harry";    $this->age=18;    $this->sex= "male";  }  function Destruct () {    echo $this->name. " <br> ";  }} $p =new person ("Zhang San", 21, "female"), $p->say ();//This is not called a clone object, because only one/* $p 1= $p at the time of destruction, $p 1->name= "John Doe", $p 1->say (); */$p 1 = Clone $p; $p 1->say ();? >

AutoLoad ()

Note: Other magic methods are added to the class, which is the only method that is not added to the class

Whenever a class is used in a page, the class name is automatically passed to the parameter as long as it is used.

<?phpfunction AutoLoad ($className) {  include "./test/". $className. ". Class.php ";}  $o =new one;  $o->fun1 ();    $t =new;  $t->fun2 ();  $h =new three;  $h->fun3 ();? >

The file in test

one.class.php

<?phpclass one{  function fun1 () {    echo "the Class one<br>";  }}? >

two.class.php

<?phpclass two{  function fun2 () {    echo "the Class two<br>";  }}? >

three.class.php

<?phpclass three{  function Fun3 () {    echo "the Class three<br>";  }}? >

Object Serialization (serialization): Converting an object to a binary string (object is stored in memory, easy to release)

Usage Time:

1. When storing objects for a long time in a database or file

2. When transferring objects to multiple PHP files

Serialize (); parameter is an object that returns the serialized binary string.

Unserialize (); The parameter is the binary string of the object, and the newly generated object is returned.

Sleep ()

Is the method that is called at serialization time

Function: It is possible to serialize a part of an object

As long as an array is returned in this method, there are several member properties in the array that serialize several member properties, and if this method is not added, all of the members are serialized

Wakeup ()

Is the method that is called when deserializing

And the process of the re-emergence of objects

<?php//defines a class of "people" class person{  var $name;  protected $age;  protected $sex;  Static $country = "China";  Declares a constant  const run= "Go";  Constructor method  function construct ($name, $age, $sex) {    $this->name= $name;    $this->age= $age;    $this->sex= $sex;  }  function say () {    echo ' my name: {$this->name}, my age: {$this->age}, my gender: {$this->sex}. <br> ";  }  function Clone () {    $this->name= "Harry";    $this->age=18;    $this->sex= "male";  }  Is the method that is called at serialization time and can partially serialize the object  function sleep () {    return Array ("name", "Age");  }  Is the method that is called at deserialization time, and is also the process of object re-birth. Can change the value inside the  function wakeup () {    $this->name= "Sanzhang";    $this->age= $this->age+1;  }  function Destruct () {  }}?>

read.php

<?php  require "11.php";    $str =file_get_contents ("Mess.txt");  $p =unserialize ($STR);  echo $p->say ();? >

write.php

<?php require "11.php";  $p =new person ("Zhang San", 18, "male");  $str =serialize ($p); File_put_contents ("Mess.txt", $str);? 

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.