我想知道在php类中工作时使用self :: method()和parent :: method()是否可接受/首选.
您可以使用$ this-> method()但$ this->也可以引用类变量,父类变量或父类中的方法.自我没有歧义::
是自我::折旧和/或使用这种风格是否有任何警告或缺点?
我理解self :: and parent ::引用类的静态实例,但在kohana中,除非你特意将方法定义为static,否则似乎没有区别.
谢谢.
添加了一个示例:假设此应用程序存储来自多个网站的论坛...
class Forum_Controller extends Controller {
function __construct()
{
parent::__construct();
}
function index()
{
echo self::categories();
}
/*
* get a list of categories from a specific site.
*/
private function categories()
{
$db = new Database;
$categories = $db->query("
SELECT * FROM
forum_categories
WHERE fk_site = '$this->site_id'
");
$view = new View('categories_view');
$view->categories = $categories;
return $view;
}
}
Run Code Online (Sandbox Code Playgroud)
此示例适用于kohana,错误报告设置为:error_reporting(E_ALL&~E_STRICT);
$ this-> site_id在主Controller_Core类(kohana中的库)中定义. …