Home  >  Article  >  Backend Development  >  Special syntax in PHP: Static, Final, Abstract and other keywords

Special syntax in PHP: Static, Final, Abstract and other keywords

PHPz
PHPzOriginal
2023-05-11 16:00:071505browse

PHP is a popular open source server-side scripting language widely used in web development. The PHP language is not only easy to learn and use, but also supports a variety of programming paradigms, object-oriented programming, functional programming, etc.

In PHP, there are some special syntax keywords, such as Static, Final, Abstract, etc. These keywords have special functions in object-oriented programming. This article will introduce these keywords in detail.

Static keyword

In PHP, the Static keyword has two uses. One is used to represent static variables and the other is used to represent static methods. The difference between static variables and static methods and ordinary variables and methods is that static variables and methods can be accessed outside the class definition, while ordinary variables and methods can only be accessed inside the class definition.

Static variables

Static variables refer to variables defined within the class definition, but their values ​​are shared between different objects. That is to say, no matter how many objects are created, the value of the static variable is always identical.

The following is an example of defining and using static variables:

class Example {

    public static $count = 0;

    public function __construct() {
        self::$count++;
    }
}

$obj1 = new Example();
$obj2 = new Example();
$obj3 = new Example();

echo Example::$count; // 输出3

In this example, the public static variable $count increments by 1 every time a class instance is created. In the last line, we can access the value of the static variable through the class name, which is output 3.

Static method

Static method refers to a method defined inside the class definition, but it can be called directly without instantiating the class.

The following is an example of defining and using static methods:

class Example {

    public static function sayHello() {
        echo "Hello World!";
    }
}

Example::sayHello(); // 输出 "Hello World!"

In this example, we define a static method sayHello. When calling, there is no need to instantiate the class but directly pass the class name and method name to call.

Final keyword

The Final keyword is used to indicate that the class or method cannot be overridden by subclasses. In other words, if a class or method is declared final, the class or method cannot be inherited or overridden.

The following is an example of using the final keyword:

final class Example {

    final public function sayHello() {
        echo "Hello World!";
    }
}

class Subclass extends Example {
    // 报错:不能继承被final修饰的类
}

class AnotherClass {

    public function sayHello() {
        echo "Hi, everyone!";
    }
}

class YetAnotherSubclass extends AnotherClass {

    final public function sayHello() {
        echo "Hello World!";
    }
}

class FinalClass extends YetAnotherSubclass {

    // 报错:不能重写被final修饰的方法
}

In this example, both the Example class and the sayHello method are declared final, and neither the subclasses SubClass nor FinalClass can inherit or override it. These classes and methods are final modified.

Abstract keyword

The Abstract keyword is used to indicate that the class or method is abstract, that is, it cannot be instantiated or implemented. Methods defined in an abstract class are only method declarations without specific implementation. These methods must be implemented concretely in subclasses.

The following is an example of using the Abstract keyword:

abstract class Animal {
    abstract public function makeSound();
}

class Cat extends Animal {

    public function makeSound() {
        echo "Meow!";
    }
}

class Dog extends Animal {

    public function makeSound() {
        echo "Woof!";
    }
}

$obj1 = new Cat();
$obj1->makeSound(); // 输出 "Meow!"

$obj2 = new Dog();
$obj2->makeSound(); // 输出 "Woof!"

In this example, an abstract class Animal is defined, which defines an abstract method makeSound. Cat and Dog subclasses must implement this method before they can be instantiated.

Conclusion

The static, final, and abstract keywords are commonly used syntax in PHP object-oriented programming and are very important tools for developers. Mastering the usage of these keywords can help us better write correct, robust and efficient PHP code.

The above is the detailed content of Special syntax in PHP: Static, Final, Abstract and other keywords. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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