About php object-oriented

WBOY
Release: 2016-07-06 13:51:17
Original
857 people have browsed it

What is the difference and usage between static attributes and ordinary attributes in object-oriented PHP

Reply content:

What is the difference and usage between static attributes and ordinary attributes in PHP object-oriented

<code>区别:
1、静态属性、静态方法是属于类的,是所有对象共有的,不属于任何一个具体的对象;
2、普通属性、普通方法是属于一个具体的对象的。

用法:
1、类外部  类名::属性名、类名::方法名() 的形式访问;
2、类内部  self::属性名、self::方法名() 的形式访问。

class Test
{
    // 静态属性
    private static $name = '测试';
    
    // 静态方法
    public static function getName ()
    {
        return self::$name;
    }
}

// 外部调用静态方法
$name = Test::getName(); // 测试</code>
Copy after login

The so-called static means that the address of this member in the memory will not change.
Then the static method of the class can be called directly without instantiating the object, because if the object is instantiated, its address will be reallocated every time an object is instantiated, then this object is in the memory The address in is dynamic.

After knowing this, you can understand that static members have their location in memory fixed when the class they belong to is loaded.

So static members do not instantiate objects, but call them directly using the class name.

Related labels:
php
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