php calls ordinary methods in this class

WBOY
Release: 2023-05-22 19:39:07
Original
429 people have browsed it

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. Calling common methods in the class
  2. Calling common methods outside the class
  3. Complete code example

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();
Copy after login

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('参数值');
Copy after login

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('类外部调用');
Copy after login

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:

参数值为:构造函数调用
参数值为:类外部调用
Copy after login

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!

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!