


Mobile app interface programming technology - learn to implement other features of the PHP class
-
Static static keyword
static. In a class, the variables or methods marked by it do not belong to any object. Use "::" when accessing. And when calling self in a class, use "self::"
For example:
<code><span><span><?php</span><span><span>class</span><span>Car</span> {</span><span>private</span><span>static</span><span>$speed</span> = <span>10</span>; <span>public</span><span><span>function</span><span>getSpeed</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$speed</span>; } <span>//在这里定义一个静态方法,实现速度累加10</span><span>public</span><span>static</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>return</span><span>self</span>::<span>$speed</span> += <span>10</span>; } } <span>$car</span> = <span>new</span> Car(); Car::speedUp(); <span>//调用静态方法加速</span><span>echo</span><span>$car</span>->getSpeed(); <span>//调用共有方法输出当前的速度值</span></span></span></code>
Static methods can also be called dynamically through variables.
<code><span>$func</span> = <span>'getSpeed'</span>; <span>$className</span> = <span>'Car'</span>; <span>echo</span><span>$className</span>::<span>$func</span>(); <span>//动态调用静态方法</span></code>
- Access control
Access control is implemented through the keywords public, protected and private. Class members defined as public can be accessed from anywhere. Class members defined as protected can be accessed by itself and its subclasses and parent classes. Class members defined as private can only be accessed by the class in which they are defined.
Class attributes must be defined as one of public, protected, and private.
Methods 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, the object is not allowed to be instantiated directly. At this time, it is generally instantiated through static methods. This method is often used in design patterns to control the creation of objects, such as singleton mode. Only one globally unique object is allowed.
<code><span><span>class</span><span>Car</span> {</span><span>private</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>echo</span><span>'object create'</span>; } <span>private</span><span>static</span><span>$_object</span> = <span>null</span>; <span>public</span><span>static</span><span><span>function</span><span>getInstance</span><span>()</span> {</span><span>if</span> (<span>empty</span>(<span>self</span>::<span>$_object</span>)) { <span>self</span>::<span>$_object</span> = <span>new</span> Car(); <span>//内部方法可以调用私有方法,因此这里可以创建对象</span> } <span>return</span><span>self</span>::<span>$_object</span>; } } <span>//$car = new Car(); //这里不允许直接实例化对象</span><span>$car</span> = Car::getInstance(); <span>//通过静态方法来获得一个实例</span></code>
- Inheritance
<code><span><span><?php</span><span><span>class</span><span>Car</span> {</span><span>public</span><span>$speed</span> = <span>0</span>; <span>//汽车的起始速度是0</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed += <span>10</span>; <span>return</span><span>$this</span>->speed; } } <span>//定义继承于Car的Truck类</span><span><span>class</span><span>Truck</span><span>extends</span><span>Car</span>{</span><span>public</span><span><span>function</span><span>speedUp</span><span>()</span> {</span><span>$this</span>->speed = <span>parent</span>::speedUp() + <span>50</span>; } } <span>$car</span> = <span>new</span> Truck(); <span>$car</span>->speedUp(); <span>echo</span><span>$car</span>->speed;</span></span></code>
- Overloading
Overloading in PHP refers to the dynamic creation of properties and methods, which is achieved through magic methods. The overloading of attributes uses __set, __get, __isset, and __unset to implement assignment, reading, determining whether the attribute is set, and destroying the attribute if it does not exist, respectively.
<code><span><span>class</span><span>Car</span> {</span><span>private</span><span>$ary</span> = <span>array</span>(); <span>public</span><span><span>function</span><span>__set</span><span>(<span>$key</span>, <span>$val</span>)</span> {</span><span>$this</span>->ary[<span>$key</span>] = <span>$val</span>; } <span>public</span><span><span>function</span><span>__get</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) { <span>return</span><span>$this</span>->ary[<span>$key</span>]; } <span>return</span><span>null</span>; } <span>public</span><span><span>function</span><span>__isset</span><span>(<span>$key</span>)</span> {</span><span>if</span> (<span>isset</span>(<span>$this</span>->ary[<span>$key</span>])) { <span>return</span><span>true</span>; } <span>return</span><span>false</span>; } <span>public</span><span><span>function</span><span>__unset</span><span>(<span>$key</span>)</span> {</span><span>unset</span>(<span>$this</span>->ary[<span>$key</span>]); } } <span>$car</span> = <span>new</span> Car(); <span>$car</span>->name = <span>'汽车'</span>; <span>//name属性动态创建并赋值</span><span>echo</span><span>$car</span>->name; </code>
Method overloading is implemented through __call. When a method that does not exist is called, the __call method will be called as a parameter. When a static method that does not exist is called, the __callStatic overload will be used.
<code>lass Car { <span>public</span><span>$speed</span> = <span>0</span>; <span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span>, <span>$args</span>)</span> {</span><span>if</span> (<span>$name</span> == <span>'speedUp'</span>) { <span>$this</span>->speed += <span>10</span>; } } } <span>$car</span> = <span>new</span> Car(); <span>$car</span>->speedUp(); <span>//调用不存在的方法会使用重载</span><span>echo</span><span>$car</span>->speed;</code>
- Class object comparison
Object comparison, when all attributes of two instances of the same class are equal, you can use the comparison operator "==" to make a judgment. When you need to judge whether two variables are the same When referencing an object, you can use the equality operator "===" to make a judgment.
<code><span><span>class</span><span>Car</span> {</span> } <span>$a</span> = <span>new</span> Car(); <span>$b</span> = <span>new</span> Car(); <span>if</span> (<span>$a</span> == <span>$b</span>) <span>echo</span><span>'=='</span>; <span>//true</span><span>if</span> (<span>$a</span> === <span>$b</span>) <span>echo</span><span>'==='</span>; <span>//false</span></code>
Object copying. In some special cases, you can copy an object through the keyword clone. At this time, the __clone method will be called, and the value of the attribute is set through this magic method.
<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>; <span>public</span><span><span>function</span><span>__clone</span><span>()</span> {</span><span>$obj</span> = <span>new</span> Car(); <span>$obj</span>->name = <span>$this</span>->name; } } <span>$a</span> = <span>new</span> Car(); <span>$a</span>->name = <span>'new car'</span>; <span>$b</span> = <span>clone</span><span>$a</span>; var_dump(<span>$b</span>); </code>
Object serialization, you can serialize the object into a string through the serialize method, which is used to store or transfer data, and then deserialize the string into an object for use through unserialize when needed.
<code><span><span>class</span><span>Car</span> {</span><span>public</span><span>$name</span> = <span>'car'</span>; } <span>$a</span> = <span>new</span> Car(); <span>$str</span> = serialize(<span>$a</span>); <span>//对象序列化成字符串</span><span>echo</span><span>$str</span>.<span>'<br>'</span>; <span>$b</span> = unserialize(<span>$str</span>); <span>//反序列化为对象</span> var_dump(<span>$b</span>); </code>
Copyright Statement: This article is the original article of the blogger and may not be reproduced without the permission of the blogger.
The above introduces the mobile app interface programming technology - learning to implement other features of the PHP class, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Shortly after Tesla launched the Model 3 Highland refresh towards the end of last year, the US federal EV tax incentive rules changed, cutting the potential discount in half for eligible buyers because of Tesla's use of Chinese LFP cells in the new M

Much has been said about flawed EV range testing methodology, but a recent test by YouTube channel Carwow (watch the video below the text) drove the point home, as none of the six electric cars in the test managed to reach the claimed range. Accordin

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

As far as EV startups go, the Rivian R1T and its sister, the R1S, have been impressively unafflicted by major issues and recalls. A new report out of Carscoops, however, shines a light on a particularly annoying and frustrating issue Rivian R1T owner

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!
