Home  >  Article  >  Backend Development  >  How to specify a method to be called in php

How to specify a method to be called in php

WBOY
WBOYOriginal
2023-05-22 21:34:06537browse

PHP is a very popular server-side scripting language commonly used for web development and the generation of dynamic web pages. In PHP, calling methods is a very important operation because it allows us to reuse code easily and allows us to complete various programming tasks more efficiently. In this article, we will discuss in detail how to specifically call a method in PHP.

1. Definition and calling of methods

In PHP, a method is a block of code used to complete a specific function. We usually define methods in classes and complete various tasks by calling methods in classes. The following is a simple PHP class definition, which contains a method called "hello":

class HelloWorld {
  public function hello() {
    echo "Hello, World!";
  }
}

In this class, we define a public method called "hello", whose function is to output a "Hello" , World!" string. Now let's see how to call this method:

$obj = new HelloWorld();
$obj->hello();

In this example, we first create an instance of the HelloWorld object $obj, and use $obj->hello() to call the hello method. After executing this code, we will see the "Hello, World!" output on the browser interface.

2. Specify calling a certain method

In PHP, we can use the keywords "::" and "->" to specify calling a certain method. The functions of these two symbols are:

  • "::": used to call static methods in the class.
  • "->": used to call instance methods in the class.

Below we will discuss the usage and examples of these two calling methods respectively.

2.1 Calling static methods

In PHP, if we want to call a static method in a class, we can use the "::" symbol to specify it. The following is a simple PHP class definition, which contains a static method called "sayHello":

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

In this class, we define a static method called "sayHello", which also outputs a " Hello, World!" string. Now let's see how to call this method:

HelloWorld::sayHello();

In this example, we first use the class name HelloWorld to specify the call to the static method sayHello. After executing this code, we will see the "Hello, World!" output on the browser interface.

2.2 Calling instance methods

In PHP, if we want to call an instance method in a class, we can use the "->" symbol to specify. We need to create an instance object of the class and then call the instance method through the instance object. The following is a simple PHP class definition, which contains an instance method called "greet":

class Person {
  public function greet() {
    echo "Hello, World!";
  }
}

In this class, we define an instance method called "greet", which also outputs a " Hello, World!" string. Now let's see how to call this method:

$person = new Person();
$person->greet();

In this example, we first create an instance of the Person object $person, and use $person->greet() to call the greet method. After executing this code, we will see the "Hello, World!" output on the browser interface.

3. Summary

In PHP, calling methods is one of the basic operations to complete various programming tasks. Through this article, we learned how to define and call methods in PHP, and introduced in detail how to specify calls to static methods and instance methods. Through this knowledge, we can more freely use various techniques and methods in PHP programming to improve our programming efficiency and effectiveness.

The above is the detailed content of How to specify a method to be called 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