Mobile app interface programming technology-learning and implementing other PHP features

Source: Internet
Author: User
: This article mainly introduces mobile app interface programming technology-other PHP features of learning and implementation. For more information about PHP tutorials, see.
  • Static keywords

    Static. The variables or methods marked by a class do not belong to any object. Use ":" for access. When self-calling in the class, use "self ::"
    For example:


  GetSpeed (); // call a common method to output the current speed value

Static methods can also be dynamically called through variables.

$ Func = 'getspeed'; $ className = 'car'; echo $ className: $ func (); // call static methods dynamically
  • Access control

Access control is implemented by the public, protected, and private keywords. It is defined as a public class member that can be accessed anywhere. A protected class member can be accessed by itself, its subclass, and its parent class. A private class member can only be accessed by the class it defines.

Class attributes must be defined as public, protected, or private.

Classes can be defined as public, private, or protected. If these keywords are not set, the method is public by default.

If the constructor is defined as a private method, the object cannot be directly instantiated. in this case, the static method is generally used to instantiate the object. in the design mode, this method is often used to control the creation of the object, for example, in Singleton mode, only one globally unique object is allowed.

ClassCar {privatefunction _ construct () {echo 'object create';} privatestatic $ _ object = null; publicstaticfunctiongetInstance () {if (empty (self: $ _ object )) {self: $ _ object = new Car (); // The internal method can call the private method. Therefore, an object can be created here} returnself ::$ _ object ;}} // $ car = new Car (); // The Object $ car = Car: getInstance () cannot be directly instantiated here; // Obtain an instance through static methods
  • Inheritance

  Speed + = 10; return $ this-> speed ;}// defines the Truck class classTruckextendsCar {publicfunctionspeedUp () {$ this-> speed = parent :: speedUp () + 50 ;}}$ car = new Truck (); $ car-> speedUp (); echo $ car-> speed;
  • Heavy load

PHP overload refers to the dynamic creation of attributes and methods, which are achieved through magic. You can use _ set ,__ get ,__ isset ,__ unset to assign values to, read, and determine whether to set or destroy a property that does not exist.

ClassCar {private $ ary = array (); publicfunction _ set ($ key, $ val) {$ this-> ary [$ key] = $ val ;} publicfunction _ get ($ key) {if (isset ($ this-> ary [$ key]) {return $ this-> ary [$ key] ;}returnnull ;} publicfunction _ isset ($ key) {if (isset ($ this-> ary [$ key]) {returntrue;} returnfalse;} publicfunction _ unset ($ key) {unset ($ this-> ary [$ key]) ;}$ car = new Car (); $ car-> name = 'auto '; // dynamically create the name attribute and assign the value echo $ car-> name;

Method overloading is implemented through _ call. when a nonexistent method is called, it is converted to a parameter call _ call method, _ callStatic is used to call a non-existent static method.

Lass Car {public $ speed = 0; publicfunction _ call ($ name, $ args) {if ($ name = 'speedup ') {$ this-> speed + = 10 ;}}$ car = new Car (); $ car-> speedUp (); // reload echo $ car-> speed to call a method that does not exist;
  • Class object comparison

Object comparison, when all the attributes of the two instances of the same class are equal, you can use the comparison operator "=" to judge, when you need to determine whether two variables are referenced by the same object, you can use the full equality operator "=" to determine.

classCar {}$a = new Car();$b = new Car();if ($a == $b) echo'==';   //trueif ($a === $b) echo'==='; //false

Object replication. in some special cases, you can use the keyword clone to copy an object. in this case, the _ clone method is called to set the attribute value through this magic method.

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

Object Serialization: you can use the serialize method to serialize an object into a string to store or transmit data, and then use the unserialize method to deserialize the string into an object.

ClassCar {public $ name = 'car';} $ a = new car (); $ str = serialize ($ a); // serialize an object to a string echo $ str .'
'; $ B = unserialize ($ str); // deserializes the object into var_dump ($ B );

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the mobile app interface programming technology-other PHP features of learning and implementation, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.