Is there no static in php5.3?

PHPz
Release: 2023-04-21 10:37:03
Original
415 people have browsed it

As PHP versions are constantly updated, some previous functions are gradually eliminated and replaced by new replacement functions. Among them, PHP 5.3 version is very old because it was released in 2009, and the programming world is developing and changing very quickly. So, does PHP 5.3 version have the static keyword? This is the topic to be discussed in this article.

As we all know, static is the keyword used to define static variables and static methods. When a static variable is defined, the variable will not be associated with an instance of the class, but will belong to the class itself. When a static method is defined, the method is also independent of the instance of the class and is called directly through the class name.

For PHP 5.3 version, it does not have the static keyword. In this version, declaring static member variables and static methods requires other code implementations.

First, declare a static member variable. You can define a static variable by using the public keyword and the $ symbol in the class, and initialize this outside the constructor. Variable:

class MyClass {
    public static $mystatic;

    function __construct() {
        // do nothing
    }
}

MyClass::$mystatic = 'Hello, world!';
echo MyClass::$mystatic; // 将输出 'Hello, world!'
Copy after login

In the above code, we use the combination of public keyword and static keyword to define a static variable $mystatic. Then, we initialize this variable outside the constructor. When outputting, we call the static variable $mystatic of this class.

The implementation of static methods is actually very simple. We can use the public keyword and the function keyword combination in the class, and then add the static keyword in front of the method name to define a static method:

class MyClass {
    public static function sayHello() {
        echo 'Hello World!';
    }
}

MyClass::sayHello(); // 调用静态方法
Copy after login

Note that static methods and static variables are related to the class itself, not to class instances. Therefore, when calling a static method or static variable, you should use the class name directly and add the :: operator in front of the method name.

Before PHP 5.3, even without the static keyword, we could use other methods to achieve similar functions. In fact, this method is mentioned in many static blogs and various documents, so it is not unfamiliar to developers who are proficient in PHP. For beginners, you can also try the above code examples to deepen your understanding of PHP.

In general, although PHP 5.3 does not have the static keyword, we can use other code to achieve similar functions. When we develop old projects with a long history, we also need to understand some old grammatical rules in order to better maintain and upgrade the project.

The above is the detailed content of Is there no static in php5.3?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!