Home  >  Article  >  Backend Development  >  How to call private method in php

How to call private method in php

藏色散人
藏色散人Original
2020-08-01 09:44:576906browse

php calls the private method: first define a parent class; then use the private function inside the class; then instantiate the parent class, turn it into an object and assign it to "$obj".

How to call private method in php

Recommended: "PHP Video Tutorial"

After instantiating a class, it becomes an object and a private function It can only be used inside the class, not outside the class, that is, on objects, nor in subclasses, such as defining a parent class

class parent{
private function _justForMyself(){
echo "这是私有函数,只能在类内部使用哦";
}
public function forAll(){
echo "这是公共函数,在类外和子类都可以使用";
echo "因为我在类内,可以调用私有函数";
$this->_justForMyself();
}
}

Now we instantiate this class and let it become a Object

$obj = new parent();//实例化parent类,让他变成一个对象并赋值给$obj
$obj->forAll();//forAll是公共函数,因此这里可以执行看到输出
$obj->_justForMyself();//_justForMyself是私有函数,类外不可使用,因此执行到这里会报错

The above is the detailed content of How to call private method 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