使用$ this,self ::,parent ::代码可读性 [英] Using $this, self::, parent:: for code readability
问题描述
我想知道如果在php类中使用self :: method()和parent :: method()是可以接受/首选。
你可以使用$ this-> method(),但$ this->也可以引用一个类变量,一个父类变量或一个父类的方法。自我没有歧义::
是自我::贬值和/或有任何注意或缺点使用这种风格吗?
我理解self ::和parent ::引用类的静态实例,但在kohana,除非你特别定义一个方法作为静态,似乎没有什么区别。 p>
感谢。
添加了一个示例:
假设此应用程序存储来自多个网站的论坛...
class Forum_Controller extends Controller {
function __construct()
{
parent ::__构造();
}
function index()
{
echo self :: 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;
}
}
此示例适用于kohana,报告设置为:
error_reporting(E_ALL&〜E_STRICT);
$ this-> site_id在Controller_Core主类(kohana中的库) 。
据我所知,$ this不应该可用,因为我以静态方式调用self :: categories(),但只有当我定义categories()为静态,它会抛出一个错误。
但是我说我更喜欢使用self ::因为从可读性的角度,这个函数应该是,而不是使用$ this这导致歧义,对我来说。
控制器不是静态在Kohana ,虽然它们可以包含静态成员变量/方法或常量。
self ::
ClassName ::
ie
class Animal
{
public static $ arms = 0;
}
class Dog extends Animal
{
public static $ leg = 0;
const NAME ='dog';
public static function bark()
{
echo'Woof';
}
}
要调用静态函数或从类获取常量请使用范围解析运算符 ::
。静态函数每个类不是每个对象。表示 ::
指的是类的静态实例是错误的,它只是一种方法来访问静态方法 - 没有一个具有这些方法的对象实例。 / p>
所以:
Dog :: bark b Dog :: $ leg,
Dog :: NAME,
/ p>
Animal :: $ arms
在类Dog中,我们可以使用 self ::
和 parent ::
不需要输入完整的类名(因为它可能很长!)
回答你的问题:否 - self: :
不会被弃用,也不是不好的做法来使用它。它不用于kohana核心的原因是一个非常不同的原因....(透明类扩展与 eval
阅读下面的更多信息...) p>
ps调用静态方法的非静态方法是错误的,不应允许 - 如果您设置 error_reporting(E_ALL | E_STRICT )
Core有一个名为
的文件: class Controller_Core {
public function someMethod(){}
}
//我们可以使用controller_Core的someMethod
pre>
Index_Controller extends Controller {}
这真的是扩展
Controller_Core
UNLESS ,你创建了MY_Controller.phpclass Controller extends Controller_Core
。// MY_Controller.php
类控制器扩展Controller_Core
{
//重载Controller_Core :: someMethod而不必更改核心文件
public function someMethod(){}
}
I would like to know if it is acceptable/preferred to use self::method() and parent::method() when working in php classes.
You can use $this->method() but $this-> can also refer to a class variable, a parent class variable, or a method from the parent class. There is no ambiguity in self::
Is self:: depreciated and/or are there any caveats or cons to using this style?
I understand that self:: and parent:: refer to a static instance of the class, but in kohana, unless you specifically define a method as static, there does not seem to be a difference.
Thanks.
Added an example: Assuming this application stores forums from multiple websites...
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; } }
This examples works in kohana with error reporting set to: error_reporting(E_ALL & ~E_STRICT);
$this->site_id is defined in the main Controller_Core class (a library in kohana).
As far as I know, $this is not supposed to be available since I am calling self::categories() in a static manner, but it is only when i define categories() as static that it throws an error.
But as I said I much rather prefer using self:: because from a readability perspective, I know exactly where this function should be, rather than using $this which causes ambiguity, to me that is.
解决方案Controllers are not static in Kohana, although they can contain static member variables / methods or constants.
self::
is a shorthand way of writingClassName::
i.eclass Animal { public static $arms = 0; } class Dog extends Animal { public static $leg = 0; const NAME = 'dog'; public static function bark() { echo 'Woof'; } }
To call static functions or get constants from a class we use the scope resolution operator
::
. Static functions are per class not per object. Saying::
refers to static instances of a class is wrong, it is just a way to access the static methods - there isn't an object instance that has these methods.so:
Dog::bark(), Dog::$leg, Dog::NAME,
we can also use
Animal::$arms
Inside the class Dog we can use
self::
andparent::
so that we do not need to type the full class name (as it could be very long!)In answer to your question though: No -
self::
is not deprecated and no it is not bad practice to use it. The reason it is not used in kohana core is for a very different reason.... (transparent class extensions witheval
read below for more info...).p.s calling non-static methods statically is wrong and shouldn't be allowed- if you set
error_reporting(E_ALL | E_STRICT)
(like you should during development) you will see an error being raised.Basically what happens is:
Core has a file called:
class Controller_Core { public function someMethod(){} }
You create:
// We can use someMethod of Controller_Core Index_Controller extends Controller {}
This is really extending
Controller_Core
UNLESS you have created MY_Controller.php which would beclass Controller extends Controller_Core
.//MY_Controller.php class Controller extends Controller_Core { // overloads Controller_Core::someMethod without us having to change the core file public function someMethod(){} }
这篇关于使用$ this,self ::,parent ::代码可读性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!