PHP Trivial Learning

Source: Internet
Author: User

    • If you define __construct in a subclass, you do not call the parent class's __construct, and if you need to call the parent class's constructor at the same time, you need to use the parent::__construct () explicit call.
1 classCar {2 function __construct () {3Print"Parent class constructor is called \ n";4    }5 }6 classTruck extends Car {7 function __construct () {8Print"The subclass constructor is called \ n";9 parent::__construct ();Ten    } One } A$car =NewTruck ();
    • Inside the class: Call property itself property $this->name and static self :: $name
    • If the constructor is defined as a private method, it is not allowed to instantiate the object directly, which is typically instantiated by a static method, which is often used in design mode to control the creation of objects, such as a singleton pattern that only allows a globally unique object.
1 classCar {2     Private function__construct () {3         Echo' Object create ';4     }5 6     Private Static $_object=NULL;7      Public Static functiongetinstance () {8         if(Empty(Self::$_object)) {9Self::$_object=NewCar ();//internal methods can call private methods, so you can create objects hereTen         } One         returnSelf::$_object; A     } - } - //$car = new Car ();//Do not allow direct instantiation of objects here the $car= Car::getinstance ();//using static methods to obtain an instance
    • Overload
      • The overloads of a property are implemented by __set,__get,__isset,__unset to assign, read, and determine whether a property is set or destroyed, respectively, for a nonexistent property.
      • The overloads of the method are implemented by __calL, and when a non-existent method is called, the __call method is called, and the __callstatic overload is used when a static method that does not exist is called.
    • Serialization of objects
      • objects can be serialized as strings by the Serialize method, used to store or pass data, and then deserialized into objects using unserialize when needed.
1 classCar {2      Public $name= ' car ';3 }4 $a=NewCar ();5 $str=Serialize($a);//object is serialized into a string6 Echo $str.‘ <br> ';7 $b=unserialize($str);//Deserializing Objects8 Var_dump($b);

    • What's the difference between a single quote and a double quote?
      •   

        PHP allows us to include it directly in a double quote string 字串变量 .

        The contents of a single quote string are always considered ordinary characters.

        Like what:

        $str = ' Hello '; echo "str is $STR"; Operation Result: STR is Helloecho ' str is $str '; Operation Result: STR is $STR

PHP Trivial Learning

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.