Home > Article > Backend Development > PHP Basics Tutorial 10: Static Properties and Static Methods
Static properties and static methods
Access Modifier
Single case mode
Magic method
Auto-loading of classes
#In the previous section, we introduced the basic use of object-oriented, but the knowledge in the previous section cannot be used in In practice, there are still problems that cannot be solved. For example, when we go to buy tickets, there is a total number of votes (defining the attribute of tickets), and a person comes to buy a ticket (ticket-1), but we create an object each time, and according to the object The method in memory is to recreate a total number of votes, which is unreasonable. Here we use the concept of static. In PHP, there are two types of static in a class:
Static properties
Static methods
Static properties are of this class Variables shared by all objects. When any object of this class accesses it, it will get the same value. Similarly, when any object of this class modifies it, it will also modify the same variable.
<?php class Ticket{ public static $number = 100; //设置总的票数是100; public $name ; public function __construct($name){ $this-> name = $name; } //定义一个方法进行买票 public function sellTicket(){ echo $this-> name . '买票了<br>'; self::$number--; //每调用一次方法总票数就减一。 } } $people1 = new Ticket('小白'); $people2 = new Ticket('小明'); $people3 = new Ticket('小华'); $people4 = new Ticket('小张'); //每个人进行买票 $people1 -> sellTicket(); $people2 -> sellTicket(); $people3 -> sellTicket(); $people4 -> sellTicket(); echo Ticket::$number; //在类的外部通过类名访问静态属性。 ......结果........ 小白买票了 小明买票了 小华买票了 小张买票了 96
You can see how static properties are defined in the above code.
访问修饰符 static $静态属性名字 = 初始化值;
Static properties can only be defined inside a class.
Outside the class
We can also access static properties in the class outside the class , as written above, direct access through the class name (this can only be done when the permission modifier is public) Ticket::$number; where:: is the scope parser.
Static properties can also be accessed through objects outside the class
$people4::$number;
Access through the class name is accessed through the scope parser::.
In the class
In the above code, we can see that in the class we access through self::$static property name. In addition to this method, there is another way to access it in the class.
Ticket::$number--;
Accessed through class name. The recommended format is through self, because this way we don’t need to modify it when our class name changes. So what is the difference between self and $this?
The difference between $this and self
In fact, as mentioned in the previous section, $this points to the current object, and here self points to the current class, one points to the object and the other points to the class, which point to different points. At the same time, they are used in different ways. self is two::, $this is ->. But the scope of application of both of them is the same, and they are both used inside the class.
Above we only explained how to define static attributes and how to use them. As for when we need to use static attributes. When we need all objects to share a piece of data during project development, we consider using static properties.
Static attributes are also an attribute, so the difference between them and ordinary attributes is:
Added the static keyword to the attribute, it will become a static attribute.
Static properties belong to the class, and properties shared by all objects
Ordinary properties belong to a single object.
Note that, just like above, static properties can be accessed in non-static methods. ;
We talked about static properties above, so let’s talk about static methods next.
<?php class Ticket{ public static $number = 100; //设置总的票数是100; public $name ; public function __construct($name){ $this-> name = $name; } public static function sayHello(){ echo '这是静态方法<br>'; } public function info(){ //在类的内部使用静态方法 self::sayHello(); //通过self访问 Ticket::sayHello();//通过类名的方式进行访问 } } $people1 = new Ticket('小白'); $people1 -> info(); $people1::sayHello(); //在类的外部通过对象名进行访问 Ticket::sayHello(); //通过类型进行访问。 ......结果........ 这是静态方法 这是静态方法 这是静态方法 这是静态方法
The static method is defined through the keyword static:
访问修饰符 static function 方法名(参数列表){ code.... }
Outside the class
The access form of static methods outside the class is the same as the method of accessing static properties (the permission modifier can only be accessed externally if it is public).
Access via class name::static method name
Access via object name::static method name (not recommended)
Pass object name->static method name. That is the form of the access method.
In a class
The way to access static methods in a class is the same as the way to access static properties
self::static method name
Class name::static method name
So under what circumstances do we use static methods? We can use static methods when manipulating static properties.
When we need to operate static attributes, we consider using
In our PHP development, we often use some patterns, such as single Example mode, factory mode, observer mode, etc. all use static methods.
Note: Static methods cannot access non-static properties;
In the above code and description, we can see that there is a public whether in front of the property or in front of the method. This public is the access modifier. One of them. Access modifiers can be said to be a method of implementing object encapsulation.
在PHP中访问修饰符可以分为三中
public 在上面的代码中我们都是使用的public,使用这种这个关键字修饰的属性和方法,不管在类的内部还是在类的内部都是可以访问的。
protected(受保护的)如果使用这个关键字修饰,那么在类的外部是不能访问。只能在类的内部进行访问。
<?php class Cat{ public $name; protected $age; public function __construct($name,$age){ $this -> name = $name; $this -> age = $age; } } $cat = new Cat('小白',4); echo $cat -> name; //在类的外部访问public echo '<br>'; echo $cat -> age; //在类的外部访问protected修饰的属性。 ......结果..... 小白 Fatal error: Cannot access protected property Cat::$age in D:\mywamp\Apache24\htdocs\zendstudio\yunsuanfu\xiushifu.php on line 16
错误的信息是说不能访问protected修饰的属性。
private(私有的),只能在类的内部使用,在外部使用会报和上面一样的错误。
这三种,后面两种看起来作用一样,都是只能在类内部使用,那又有什么区别呢?现在看来,并没有区别,但是学过类的继承,那么这两种还是有区别的。
访问修饰符的使用:
成员属性必须制定访问修饰符,不然会报错
方法前面可以不写修饰符,默认是public
静态属性可以不指定访问修饰符,默认是public
上面讲解到我们什么时候使用到静态方法。在一些设计模式中,我们可以使用到静态方法和静态属性。
设计模式:是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的;设计模式使代码编制真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。(百度)
在开发的时候,我们有这样的需求,在代码中我们创建一个对象,但是我们希望在一个项目中,这个对象实例只能有一个,不能创建多个对象,从而实现对数据库等资源的保护。这时候就使用到单例模式。
<?php class DaoMysql{ public $link; //模拟数据库连接 private static $instance;//当前类的对象。 private function __construct(){ echo '数据库连接<br>'; } public static function getInstance(){ if(self::$instance == null){ self::$instance = new DaoMysql(); } return self::$instance; } public function insertSql(){ echo '添加数据<br>'; } } $daoMysql = DaoMysql::getInstance(); $daoMysql -> insertSql(); $daoMysql2 = DaoMysql::getInstance(); $daoMysql2 -> insertSql(); ......结果....... 数据库连接 添加数据 添加数据
1. 既然是单例模式,那么就不能在外部创建对象,这就要把构造函数用private修饰(创建对象要调用构造函数,这里把构造函数私有化,调用不起来),这样在外部就不能创建对象。
2. 我们在类里面创建了一个对象的引用$instance,在类里面创建对象,这是允许的。
3. 在类中定义一个静态方法,我们就是通过这个方法来创建对象,通过类名::方法名进行创建,进去后先判断$instance是否为空,只有如空的时候我们才进行创建。然后返回对象。
4. 因为要在静态方法中访问属性,那么这个属性就应该是静态的属性。
5. 在类的外部通过类::静态方法名进行对象的创建。
6. 在结果中我们可以看到我们有两个对象,但是构造方法在第二次没有执行,说明对象没有创建。
虽然在上面我们做了很多限制,但是在PHP中还是有方法的到更过的对象,克隆和继承。
在上面的静态方法中判断对象是否创建还有一种方法。
if(!(self::$instance instanceof self)){ self::$instance = new DaoMysql(); }
其中instanceof就是类型运算符。 根据帮助文档,它有几个作用
用于确定一个 PHP 变量是否属于某一类 class 的实例:
可用来确定一个变量是不是继承自某一父类的子类的实例:
也可用于确定一个变量是不是实现了某个接口的对象的实例:
上面的代码中self代表当前的类。instanceof判断前面的变量是否是后面类的实例,然后取反。
在PHP中有一些定义在类中的神奇的方法,称为魔术方法。具体的魔术的方法的使用可以看另外一篇博客
PHP的魔术方法
在前面我们讲过文件的引入,使用include和require这两种类型。在开发中我们有时需要引入大量的文件,可以是10个,也可能是20个,如果还是使用原来的方法,累人。
在 PHP 5 中,不再需要这样了。可以定义一个 __autoload() 函数,它会在试图使用尚未被定义的类时自动调用。
而我们在写类的时候,一般都是一个类一个文件,而文件的名字我们一般是类名.class.php的格式。
<?php //自动加载的方法,当我们使用这个文件不存在的类的时候,就会自动加载。 function __autoload($class_name){ require_once './' . $class_name . '.class.php'; } $dao = new Dao('小白',5); $cat = new Cat('小花',2); $dao -> eat(); $cat -> eat();
__autoload($类名),在个函数不是写在类中的,所以前面是没有权限修饰符。
上面的自动加载方式是有局限性的,当文件是在不同的文件夹中的时候,这种方法显然是不行的。这时候可以创建一个数组,把类名当做键,对应的路径当成值,进行存储。自动加载的时候就能正确的引入。
<?php $path = array( 'Dao' => './dao/Dao.class.php', 'Cat' => './cat/Cat.class.php' ); //自动加载的方法,当我们使用这个文件不存在的类的时候,就会自动加载。 function __autoload($class_name){ global $path; require_once $path[$class_name]; } $dao = new Dao('小白',5); $cat = new Cat('小花',2); $dao -> eat(); $cat -> eat();
可以看到在前面定义了一个数组用来存储路径。
注意:在函数中使用global声明一下,才能使用全局变量。
在面向对象中用到静态属性和静态方法的时候还是很多的。同时权限修饰符在面向对象中是很重要的,因为我们通过修饰符控制访问权限。魔术方法的掌握,也可以让我们在访问中明白各种调理机制。
以上就是PHP基础教程十之静态属性和静态方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!