Home Backend Development PHP Tutorial Mobile app interface programming technology - learn to implement other features of the PHP class

Mobile app interface programming technology - learn to implement other features of the PHP class

Jul 30, 2016 pm 01:31 PM
car gt speed this

  • 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>
Copy after login

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>
Copy after login
  • 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>
Copy after login
  • 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>
Copy after login
  • 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>
Copy after login

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>
Copy after login
  • 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>
Copy after login

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>
Copy after login

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>
Copy after login

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1660
14
PHP Tutorial
1260
29
C# Tutorial
1233
24
What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

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

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

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

Deal | Tesla Model 3 Long Range AWD regains full $7,500 tax incentive, drops to below $40,000 Deal | Tesla Model 3 Long Range AWD regains full $7,500 tax incentive, drops to below $40,000 Jun 19, 2024 am 09:55 AM

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

EV highway range test reveals Model 3 is not alone in range overestimation — Mercedes EQE leads the pack as BMW i5 disappoints EV highway range test reveals Model 3 is not alone in range overestimation — Mercedes EQE leads the pack as BMW i5 disappoints Jun 22, 2024 am 10:09 AM

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

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

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提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

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

Rivian upper control arm failures alarm owners - service centres seemingly to blame Rivian upper control arm failures alarm owners - service centres seemingly to blame Jun 25, 2024 pm 03:39 PM

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

Let's talk about why Vue2 can access properties in various options through this Let's talk about why Vue2 can access properties in various options through this Dec 08, 2022 pm 08:22 PM

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!

See all articles