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

How to reference private methods internally in php

coldplay.xixi
Release: 2023-03-06 12:34:01
Original
3101 people have browsed it

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

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!

Related labels:
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