PHP knowledge points

Source: Internet
Author: User

PHP knowledge points
1. Class Definition


  items[$artnr += $num;   }}

A class cannot be defined separately in multiple files, nor can the class be defined into multiple PHP blocks (internal functions can be divided ).
The following classes cannot be defined:
StdClass
_ Sleep
_ Wakeup
In fact, do not start with _ to define a class.

2. Constructor
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. . . */    }}

If the class has no constructor, the base class constructor is called.
The default value can be assigned to the constructor parameters.


  Add_item ($ item, $ num) ;}// buy 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 in constructors.

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

Class uses $ this internally to represent itself.

4. class-related functions

_ Autoload-try to load undefined classes
Call_user_method_array-call a user method and pass the parameter array (obsolete)
Call_user_method-call the user Method for a specific object (obsolete)
Class_alias-create an alias for a class
Class_exists-check whether the class is defined
Get_called_class-name of the Static Binding ("Late Static Binding") Class
Get_class_methods-returns an array composed of class method names.
Get_class_vars-returns an array composed of the default attributes of the class.
Get_class-Class Name of the returned object
Get_declared_classes-returns an array composed of the names of defined classes.
Get_declared_interfaces-returns an array containing all declared interfaces.
Get_declared_traits-returns an array of all defined traits.
Get_object_vars-returns an associated array composed of object attributes.
Get_parent_class-return the parent class name of the object or Class
Interface_exists-check whether the interface has been defined
Is_a-if the object belongs to this class or the class is the parent class of this object, TRUE is returned.
Is_subclass_of-if this object is a subclass of this class, TRUE is returned.
Method_exists-check whether the class method exists
Property_exists-check whether the object or class has this attribute
Trait_exists-check whether the specified trait exists

5. Inheritance

  owner = $name;    }}?>

PHP does not support multi-inheritance.

6. Static Method

  ;}} Class B extends A {function example () {echo I am the redefined function B: example ().; a: example () ;}// Class A has no object, which will output // I am the original function A: example (). a: example (); // create A class B object $ B = new B; // This will output // I am the redefined function B: example (). // I am the original function A: example (). $ B-> example ();?>
7. base class references parent

  ;}} Class B extends A {function example () {echo I am B: example () and provide additional functionality .; parent: example () ;}}$ B = new B; // This will call B: example (), and it will call A: example (). $ B-> example ();?>
8. serialization

  One ;}}// page1.php: include (classa. inc); $ a = new A; $ s = serialize ($ a); // store $ s somewhere so that page2.php can find $ fp = fopen (store, w); fwrite ($ fp, $ s); fclose ($ fp); // page2.php: // This line of include (classa. inc); $ s = implode (, @ file (store); $ a = unserialize ($ s); // you can use show_one () of $ a object now () function $ a-> show_one ();?>
9. Magic function _ sleep _ wakeup10. method 1 of object attribute access in array Mode

Function obj2array (obj ){
Return new ArrayObject (obj, ArrayObject: ARRAY_AS_PROPS );
}
This method is relatively simple. It is more complicated to inherit ArrayAccess from another method.

11. Convert array to object
/*** Convert an array to an 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 in redis:

/*** Serialized object, returns $ 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.