";]."/> ";].">

Home  >  Article  >  Backend Development  >  How to reference private methods internally in php

How to reference private methods internally in php

coldplay.xixi
coldplay.xixiOriginal
2020-10-06 11:06:433081browse

How PHP refers to private methods internally: When calling the [get()] method, add [$this] in front, the code is [$con = $this->get($name, $age);echo "my name is :".$name."
";].

How to reference private methods internally in php

How php refers to private methods internally:

This code is very simple, I want to implement the class Internal private method calls. As shown in line 4. I was used to writing JS in the past. When calling the get() method, if I didn't add "$this" in front, the program would have problems. After filling it in, you can call it.

<?php
class Person {
    public function say($name, $age) {
        $con = $this->get($name, $age); //这里的$this->是关键
        echo "my name is :".$name."<br />";
        echo "my age is :".$age."<br/>";
        echo "get :".$con."<br/>";
    }
    private function get($a, $b) {
        $a = $a.$b;
        return $a;
    }
}
$person=new Person();
$person->say("ren", 25);
?>

Related free learning recommendations: php programming (video)

The above is the detailed content of How to reference private methods internally 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

Related articles

See more