Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道,是否有办法在另一个类函数中使用一个类函数而不使用 using $this->,例如:
$this->
class Example { public function sayhi() { echo "HI"; } public function hi() { sayhi(); } }
像这样的东西?
不,不是。PHP 知道函数,因此无法区分sayhi()是函数还是方法。
sayhi()
你可以这样做
<?php class test { public function hi() { self::say(); } private function say() { echo 'hi'; } } $t = new test(); $t->hi(); ?>
所以是的,有一种方法可以避免 $this,但我不推荐它。