Home  >  Article  >  Backend Development  >  Why can't classes and methods be called statically externally in php?

Why can't classes and methods be called statically externally in php?

PHPz
PHPzOriginal
2023-03-24 17:09:591268browse

PHP is a widely used programming language, characterized by its concise and easy-to-use syntax, rapid development speed and support for multiple databases. However, some PHP developers may encounter a problem: classes and methods cannot be called statically externally. This article will explore this problem and provide a solution.

What is static call?

In PHP development, static calling is a way to directly call a class or method without instantiating the object. The sample code is as follows:

class Car{
   public static function run(){
      echo "the car is running";
   }
}

// 静态调用
Car::run();

As mentioned above, static calling is a way to access class members (methods or properties) by using the class name and the double colon operator.

Why can't it be called statically externally?

In PHP, the main reason why it cannot be called externally statically is the restriction of access modifiers. If a class or method is defined with the private or protected modifier, its members can only be accessed within the class. Therefore, when called externally, the PHP interpreter will deny the corresponding access request.

Solution:

If you want to call classes or methods statically externally, you need to define them as public (using the public modifier). For example, suppose we have a class called MyClass with a public static method myMethod, which can be defined as follows:

class MyClass{
   public static function myMethod(){
      echo "Hello World!";
   }
}

After defining the method in the class, it can be directly Call it without instantiating the class. For example, the method can be called as follows in other PHP files:

MyClass::myMethod();

Additionally, the reflection API can be used for static access. The Reflection API is a powerful tool that can be used to obtain information about classes and objects while PHP is running. The following code demonstrates how to use the reflection API to statically instantiate a class and call its methods.

class Example{
   public function run(){
       echo "example code";
   }
}

// 使用反射API静态地调用run()函数
$class = new ReflectionClass('Example');
$method = $class->getMethod('run');
$method->invoke(null);

In the above code example, the reference to the run() method is obtained by using the ReflectionClass class and the getMethod() method. Then, use the invoke() method to call the method. Note that since there is no need to instantiate the Example class here, a null value is passed in invoke().

Conclusion

In PHP development, external static calls to classes or methods may be restricted by access modifiers. To solve this problem, you need to define the class or method as public and use the class name and the double colon operator to access the class members. In addition, the reflection API can also be used for static access. Knowing this information can help developers better utilize the power of PHP.

The above is the detailed content of Why can't classes and methods be called statically externally in php?. 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