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".
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!