Mobile App Interface Programming Technology-Learn other PHP class features

Source: Internet
Author: User
Tags object serialization php class
    • Static statically-keyword

      Static. In a class, a variable or method that is marked by him does not belong to any one object. Use "::" When you visit. And when invoking itself in a class, use "Self::"
      For example:


    
     classCar {privatestatic$speed = 10;    publicfunctiongetSpeed() {returnself::$speed;    }    //在这里定义一个静态方法,实现速度累加10publicstaticfunctionspeedUp()    {returnself::$speed += 10;    }}$car = new Car();Car::speedUp();  //调用静态方法加速echo$car->getSpeed();  //调用共有方法输出当前的速度值

Static methods can also be dynamically called through variables.

$func'getSpeed';$className'Car';echo$className::$func();  //动态调用静态方法
    • Access control

Access control is implemented by keyword public,protected and private. Class members that are defined as public can be accessed from anywhere. A class member that is defined as protected can be accessed by itself and its subclasses and parent classes. A class member that is defined as private can only be accessed by the class in which it is defined.

Class properties must be defined as public, protected, and private.

A method in a class can be defined as public, private, or protected. If these keywords are not set, the method defaults to public.

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.

classCar {privatefunction__construct() {echo'object create';    }    privatestatic$_objectnull;    publicstaticfunctiongetInstance() {if (empty(self::$_object)) {            self::$_objectnew Car();             //内部方法可以调用私有方法,因此这里可以创建对象        }        returnself::$_object;    }}//$car = new Car(); //这里不允许直接实例化对象$car//通过静态方法来获得一个实例
    • Inherited

    
     classCar {public$speed = 0; //汽车的起始速度是0publicfunctionspeedUp() {$this->speed += 10;        return$this->speed;    }}//定义继承于Car的Truck类classTruckextendsCar{publicfunctionspeedUp() {$this->speed = parent::speedUp() + 50;    }}$car = new Truck();$car->speedUp();echo$car->speed;
    • Overload

Overloading in PHP refers to the dynamic creation of properties and methods, which are implemented by magic methods. 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.

 classCar {Private$ary=Array(); Public function__set($key, $val) {$this->ary[$key] =$val; } Public function__get($key) {if(isset($this->ary[$key])) {return$this->ary[$key]; }returnNULL; } Public function__isset($key) {if(isset($this->ary[$key])) {returntrue; }returnfalse; } Public function__unset($key) {unset($this->ary[$key]); }}$car=NewCar ();$car->name =' car ';//name properties are dynamically created and assigned valuesEcho$car->name;

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.

lass Car {    public$speed0;    publicfunction__call($name, $args) {if ($name'speedUp') {            $this10;        }    }}$carnew Car();$car//调用不存在的方法会使用重载echo$car->speed;
    • Class object Comparison

Object comparison, when all the properties of two instances of the same class are equal, you can use the comparison operator "= =" to determine whether the two variable is a reference to the same object, you can use the congruent operator "= = =" to judge.

classCar {}$anew Car();$bnew Car();if ($a$becho'==';   //trueif ($a$becho'==='//false

Object replication, in some special cases, you can copy an object by using the keyword clone, at which point the __clone method is called, and the value of the property is set by this magic method.

classCar {public$name'car';    publicfunction__clone() {$objnew Car();        $obj$this->name;    }}$anew Car();$a'new car';$bclone$a;var_dump($b);

Object serialization, which can be serialized as a string by the Serialize method, used to store or pass data, and then deserialize the string into an object when needed by Unserialize.

classCar {public$name'car';}$anew Car();$str = serialize($a//对象序列化成字符串echo$str.'
';$b = unserialize($str//反序列化为对象var_dump($b);

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

The above describes the mobile app interface programming technology-learning to implement the PHP class other features, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.