Home > Backend Development > PHP Tutorial > Mobile app interface programming technology - advanced PHP classes and objects for learning and implementation

Mobile app interface programming technology - advanced PHP classes and objects for learning and implementation

WBOY
Release: 2016-08-08 09:21:54
Original
1273 people have browsed it

  • Create classes and objects
<code><span><span><?php</span><span><span>class</span><span>Car</span>
{</span><span>public</span><span>$name</span> = <span>'汽车'</span>;

   <span>public</span><span><span>function</span><span>getName</span><span>()</span>
   {</span><span>return</span><span>$this</span>->name;
   }
}

<span>$car</span> = <span>new</span> Car();

<span>echo</span><span>$car</span>->name.<span>'<br>'</span>;
<span>echo</span><span>$car</span>->getName();</span></span></code>
Copy after login
  • Attributes of a class

The attribute declaration starts with the keywords public, protected or private, followed by an ordinary variable declaration. Attribute variables can be initialized with a default value, and the default value must be a constant.

The meaning of access control keywords is:
public: public
protected: protected
private: Private ones

are public by default and can be accessed by the outside world. Generally, the properties or methods of an object are accessed through the -> object operator. For static properties, use :: double colon to access them. When called inside a class member method, you can use the $this pseudo variable to call the properties of the current object.

Protected properties and private properties do not allow external calls, but can be called within the member methods of the class.

  • Class methods

Like attributes, class methods also have public, protected and private access control.

Modified with the keyword static, it is called a static method. Static methods do not need to instantiate objects and can be called directly through the class name. The operator is a double colon::.

<code><span><span><?php</span><span><span>class</span><span>Car</span> {</span><span>public</span><span>$speed</span> = <span>0</span>;
    <span>//增加speedUp方法,使speed加10</span><span><span>function</span><span>speedUp</span><span>()</span>
    {</span><span>$this</span>->speed +=<span>10</span>;
    }

   <span>public</span><span>static</span><span>$name</span> = <span>'jarlen'</span>;

   <span>public</span><span>static</span><span><span>function</span><span>getSpeed</span><span>()</span>
   {</span><span>return</span><span>'ja'</span>;
   }

}
<span>$car</span> = <span>new</span> Car();
<span>$car</span>->speedUp();
<span>echo</span><span>$car</span>->speed;

<span>echo</span><span>'<br><br>'</span>.Car::<span>$name</span>.<span>'<br><br>'</span>;

<span>echo</span> Car::getSpeed();</span></span></code>
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the mobile app interface programming technology - PHP advanced classes and objects for learning and implementation, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template