In PHP, we usually define some common methods in a class to complete some common functions and operations. Among these methods, some methods need to be called inside the class, while some methods need to be called outside the class. For these two different calling requirements, we need to use different methods to implement them.
This article will introduce how to call the common methods in this class in PHP. It mainly includes the following content:
1. Call ordinary methods in a class
To call ordinary methods in a class, we can use the "$this" keyword to refer to the current class object. For example, to call a common method in the constructor of a class, you can use the following code:
class MyClass{ //定义一个普通方法 public function myMethod($arg){ //方法体 } //类的构造函数 public function __construct(){ //在构造函数中调用myMethod()方法 $this->myMethod('参数值'); } } $obj = new MyClass();
In the above code, we use "$this->myMethod()" in the constructor of the class The ordinary method "myMethod()" defined in the class is called.
It should be noted that when calling ordinary methods, we need to use the "->" operator to connect the class object and the method name. At the same time, if the method has parameters, we need to put the parameter list in parentheses "()" after the method name.
2. Call ordinary methods outside the class
To call ordinary methods in this class outside the class, we need to instantiate the class first and obtain the object of the class before making the call. Different from calling within a class, we need to use the class object name instead of the "$this" keyword to indicate the current class object.
For example, to call a common method in a class outside the class, you can use the following code:
class MyClass{ //定义一个普通方法 public function myMethod($arg){ //方法体 } } //实例化MyClass类 $obj = new MyClass(); //调用MyClass类中的myMethod()方法 $obj->myMethod('参数值');
In this example, we instantiate the MyClass class and obtain the class object" $obj" to call the ordinary method "myMethod()" defined in the class.
It should be noted that unlike using the "->" operator when calling ordinary methods inside a class, we also need to use the "->" operator to connect class objects when calling outside the class. and method name.
3. Complete code example
The following is a complete code example for calling ordinary methods in this class in PHP:
class MyClass{ //定义一个普通方法 public function myMethod($arg){ //方法体 echo "参数值为:" . $arg; } //类的构造函数 public function __construct(){ //在构造函数中调用myMethod()方法 $this->myMethod('构造函数调用'); } } //实例化MyClass类 $obj = new MyClass(); //调用MyClass类中的myMethod()方法 $obj->myMethod('类外部调用');
In this example, we define a name is the class "MyClass" and defines a normal method named "myMethod()" in it. We call this method in the constructor of the class and outside the class respectively, and pass in different parameter values.
When we execute this program, the following results will be output:
参数值为:构造函数调用 参数值为:类外部调用
The above are the methods and code examples for calling ordinary methods in this class in PHP. Through these practices and learning, we can have a deeper understanding of the concepts of classes and methods in PHP, and how to use them in actual development to improve code reusability and maintainability.
The above is the detailed content of php calls ordinary methods in this class. For more information, please follow other related articles on the PHP Chinese website!