Some knowledge points of PHP class

Source: Internet
Author: User
Tags foreach define array object definition explode php class sleep


1. Definition of class


items[$artnr += $num;
   }
}


You cannot define a class separately in multiple files, nor can you divide the class definition into multiple PHP blocks (within which the function can be divided).
You cannot define a class that is named the following:
StdClass
__sleep
__wakeup
In fact don't define classes with __.



2. Constructor function


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 a default value


add_item ($item, $num);
    }
}

// Buy some of the same boring old goods
$default_cart = new Constructor_Cart;
// Buy some real goods...
$different_cart = new Constructor_Cart(20, 17);
?>


@new can suppress errors that occur in constructors.



3. Use of Class


$cart = new Cart;
$cart->add_item(10, 1);


Class uses $this to represent itself.



4. Class Correlation function



__autoload-attempt to load an undefined class
Call_user_method_array-calls a user method, passing a parameter array (deprecated)
Call_user_method-invokes a user method on a specific object (deprecated)
class_alias-Create an alias for a class
class_exists-Check whether a class is defined
get_called_class-the name of the late static binding ("Late static Binding") class
get_class_methods-returns an array of the class's method names
get_class_vars-returns an array of the default properties of the class
get_class-returns the class name of the object
get_declared_classes-returns an array of the names of the defined classes
get_declared_interfaces-returns an array containing all declared interfaces
get_declared_traits-returns an array of all the 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 the class is the parent of this object
is_subclass_of-returns TRUE If this object is a subclass of the class
method_exists-Check if the method of the class exists
property_exists-check whether an object or class has this property
trait_exists-check whether the specified trait exists



5. Inheritance


owner = $name;
    }
}
?>


PHP does not support multiple inheritance.



6. Static method


One

  }//Page1.php:include (CLASSA.INC);
  $a = new A;
  $s = serialize ($a);
  Storing $s somewhere so that page2.php can find $fp = fopen (store, W);
  Fwrite ($fp, $s);

Fclose ($FP);

  page2.php://For normal solution serialization requires this line include (CLASSA.INC);
  $s = implode (, @file (store));

  $a = unserialize ($s); You can now use the Show_one () function of the $a object $a->show_one ();?>


9. Magic function __sleep __wakeup



10. Allow array access to object properties



Method 1

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




11. Array to turn objects


/**
     * Array to object * @param unknown $e
     * @return voidStdClass
     */
    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 serialization and deserialization of its own implementation



It is more convenient to use in Redis:


/**
     * Serialize the object, returning a $json string
     */
    public static function serialize($model){
        //return serialize($model);
        if(!$model)return '{}';
        $json='{';
        foreach($model as $key2=>$value2){
            if($json!='{')$json.=',';
            $json.=$key2:$value2;
        }
        $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);
            $temparr1=explode(':',$temparr[0]);

            if(count($temparr1)==0)continue;
            $result[$temparr1[0]]=trim( $temparr1[1],'');
        }
        //$obj=  (object)($result);

        return obj2array($result);
        //return $result;
    }


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.