[英]Joomla JDatabase: selectRowNumber strange use of class attribute or method
in Joomla 3.9.16 there is a strage use of a class attribute that is also used as a method!在 Joomla 3.9.16 中,巧妙地使用了 class 属性,该属性也用作方法!
In libraries/joomla/database/query.php you can find:在 library/joomla/database/query.php 你可以找到:
protected $selectRowNumber = null;
but also但是也
public function selectRowNumber($orderBy, $orderColumnAlias)
{
$this->validateRowNumber($orderBy, $orderColumnAlias);
$this->select("ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias");
return $this;
}
You can undestand why reading this method of the same class:您可以理解为什么要阅读相同的 class 的这种方法:
protected function validateRowNumber($orderBy, $orderColumnAlias)
{
if ($this->selectRowNumber)
{
throw new RuntimeException("Method 'selectRowNumber' can be called only once per instance.");
}
$this->type = 'select';
$this->selectRowNumber = array(
'orderBy' => $orderBy,
'orderColumnAlias' => $orderColumnAlias,
);
}
When you call $this->selectRowNumber()
as a method for the first time, it calls $this->validateRowNumber()
in which $this->selectRowNumber()
becomes an array!当您第一次将
$this->selectRowNumber()
作为方法调用时,它会调用$this->validateRowNumber()
其中$this->selectRowNumber()
变成一个数组! If you call again $this->selectRowNumber()
it throws an exception because you can call it only once and is no more a method.如果你再次调用
$this->selectRowNumber()
它会抛出一个异常,因为你只能调用它一次并且不再是一个方法。
I wonder if this is a good programming practice, I think definitely NOT because is not easy to understand and mantain.我想知道这是否是一个好的编程实践,我认为绝对不是,因为它不容易理解和维护。 Maybe you can achive the same result in another way much more clear and linear.
也许您可以用另一种更清晰和线性的方式获得相同的结果。 What I ask is: am I right or this is a common practice?
我要问的是:我是对的还是这是一种常见的做法?
Thanks谢谢
Are you saying that you can reproduce this fault?你是说你能重现这个故障吗? I am going to assume that you are misreading the script.
我将假设您误读了脚本。
It looks to me that the selectRowNumber()
method is called the first time and then inside validateRowNumber()
, the class variable/property $this->selectRowNumber
is checked for truthiness.在我看来,第一次调用
selectRowNumber()
方法,然后在validateRowNumber()
内部检查 class 变量/属性$this->selectRowNumber
的真实性。
Since it is null
(falsey) the first time, no exception is thrown.由于第一次是
null
(falsey),所以没有抛出异常。
The class variable/property (not the method) is updated with class 变量/属性(不是方法)更新为
$this->selectRowNumber = array(
'orderBy' => $orderBy,
'orderColumnAlias' => $orderColumnAlias,
);
Then back inside selectRowNumber()
, the ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias
string is applied to $this->select()
.然后回到
selectRowNumber()
内部,将ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias
字符串应用于$this->select()
。
Because ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias
must not be applied twice to a given query, the safeguard / thrown Exception checks for a truthy class variable/property -- which of course it is because it has been modified from null
to a non-empty associative array.因为
ROW_NUMBER() OVER (ORDER BY $orderBy) AS $orderColumnAlias
不能两次应用于给定查询,所以安全/抛出的异常会检查真实的 class 变量/属性——这当然是因为它已从null
到非空关联数组。
Is it confusing to have properties and methods sharing the same name?属性和方法共享相同的名称是否令人困惑? Sure.
当然。 The only distinction between the two is the trailing
()
.两者之间的唯一区别是尾随
()
。
Is this acceptable coding practice?这是可接受的编码实践吗? Well, in terms of readability it's not ideal.
好吧,就可读性而言,它并不理想。 But once a project standardizes its naming convention, sometimes these kinds of converging names happen.
但是一旦一个项目标准化了它的命名约定,有时就会出现这种融合的名称。 The bigger the code base, the more likely this is to occur.
代码库越大,发生这种情况的可能性就越大。 I think consistency is more important than readability in this isolated situation.
我认为在这种孤立的情况下,一致性比可读性更重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.