Some knowledge points of the PHP class

Source: Internet
Author: User
Tags deprecated explode php class

1. Definition of class
<?phpclass Cart{   var $items;   function add_item($artnr,$num){      $this->items[$artnr += $num;   }}

A class cannot be defined in more than one file, nor can a class definition be divided into multiple PHP blocks (inside a function).
You cannot define a class named the following:
StdClass
__sleep
__wakeup
In fact, do not define classes with __.

2. Constructors
 class Cart {    var $todays _date;var $name;var $owner;var $items=Array("VCR","TV"); function Cart() {        $this->todays_date = Date ("Y-m-d");$this->name =$GLOBALS[' FirstName '];/ * Etc ... * /}}

Class if there is no constructor, the base class constructor is called.
Constructor parameters can be assigned default values

<?php class constructor_cart extends Cart {     function constructor_cart($item = "Ten", $num = 1) {        $this->add_item ($item,$num); }}//Buy some of the same boring old goods$default _cart=NewConstructor_cart;//Buy some real goods ...$different _cart=NewConstructor_cart ("a", -);?>

@new can suppress errors that occur in the constructor.

3. Use of Classes
$cart=new Cart;$cart->add_item("10"1);

Within the class, use $this to represent itself.

4. Class-related functions

__autoload-attempting to load an undefined class
call_user_method_array-invokes a user method, passing a parameter array (deprecated)
Call_user_method-invoking a user method on a specific object (deprecated)
class_alias-Creating an alias for a class
class_exists-checking whether a class is defined
get_called_class-Late static binding ("late static binding") class name
get_class_methods-returns an array of the method names of the class
get_class_vars-returns an array that consists of the default properties of the class
get_class-returns the class name of the object
get_declared_classes-returns an array consisting of the names of the defined classes
get_declared_interfaces-returns an array containing all the declared interfaces
get_declared_traits-returns an array of all defined traits
get_object_vars-returns an associative array consisting of object properties
get_parent_class-returns the parent class name of an object or class
interface_exists-Check if the interface has been defined
is_a-returns TRUE if the object belongs to the class or if the class is the parent class of this object
is_subclass_of-returns TRUE If this object is a subclass of the class
method_exists-checking the existence of a class's methods
property_exists-checking whether an object or class has this property
trait_exists-checks if the specified trait exists

5. Inheritance
<?phpclass Named_Cart extends Cart {    var$owner;    function set_owner ($name) {        $this$name;    }}?>

PHP does not support multiple inheritance.

6. Static methods
<?php class A {     function example() {        Echo "I am the original function a::example (). <br/>\n"; }} class B extends A {     function example() {        Echo "I am the redefined function b::example (). <br/>\n";    A::example (); }}The //class A does not have an object, which outputs//I AM the original function a::example (). <br/>A::example ();//Create an object of class B$b=NewB//This will output//I AM the redefined function b::example (). <br/>//I AM the original function a::example (). <br/>$b->example ();?>
7. base class Reference Parent
<?php class A {     function example() {        Echo "I am a::example () and provide basic functionality.<br/>\n"; }} class B extends A {     function example() {        Echo "I am b::example () and provide additional functionality.<br/>\n";Parent:: Example (); }}$b=NewB//This will call B::example (), and it will go to call A::example (). $b->example ();?>
8. Serialization
<?php//Classa.inc:   class A {      var $one=1; function show_one() {          Echo $this->one; }  }//page1.php:  include("Classa.inc");$a=NewA$s= Serialize ($a);//Store $s somewhere so page2.php can find  $fp= fopen ("Store","W"); Fwrite$fp,$s); Fclose$fp);//page2.php:  //This line is required for normal deserialization  include("Classa.inc");$s= Implode ("", @file ("Store"));$a= Unserialize ($s);//You can now use the Show_one () function of $a object.  $a->show_one ();?>
9. Magic function __sleep __wakeup10. Allow array mode access to object Properties Method 1

function Obj2array (obj) {
return new Arrayobject (obj, arrayobject::array_as_props);
}
This method is relatively simple, and another way to inherit arrayaccess is a bit more complicated.

11. Array to Object
    /** * Array to Object * @param Unknown $e * @return void| StdClass * *     Public Static  function arraytoobject($e){        if(GetType ($e)!=' array ')return;foreach($e  as $k=$v){if(GetType ($v)==' array '|| GetType ($v)==' object ')$e[$k]= (object) Arraytoobject ($v); }return(object)$e; }
12 self-implemented serialization and deserialization

Easy to use with Redis:

    /** * Serialize object, return $json String * /     Public Static  function serialize($model){        //return Serialize ($model);        if(!$model)return ' {} ';$json=' {';foreach($model  as $key 2=$value 2){if($json!=' {')$json.=', ';$json.=" $key 2:\" $value 2\ ""; }$json.='} ';return $json; } Public Static  function unserialize($json){        $json=str_replace (' {',"',$json);$json=str_replace ('} ',"',$json);$array=explode (', ',$json);$result=[];foreach($array  as $key=$value){$temparr=explode (', ',$value);$temparr 1=explode (': ',$temparr[0]);if(Count ($temparr 1)==0)Continue;$result[$temparr 1[0]]=trim ($temparr 1[1],' "'); }//$obj = (object) ($result);        returnObj2array ($result);//return $result;}

Reference:
http://php.net/manual/zh/oop4.php

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Some knowledge points of the PHP class

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.