如何使用 __call 动态返回属性值
我一直在尝试想出一种使用 __call 动态返回类的属性值的方法,而不是创建大量其唯一目的是返回这些值的函数。我的想法是能够请求(例如)$this->fetch_PersonFirstName()并让类检查是否$this->person["first_name"]< /strong> 设置并返回值。另一个例子是调用 $this->fetch_BookGenreTitle() 并让类返回 $this->book["genre"]["title"] 的值>。我知道这样的事情必须做一些检查才能自动确定,例如,由于 $this->book["genre_title"] 不存在,那么它应该检查$this->book["genre"]["title"]。
到目前为止,我已经提出了代码(出于某种原因)可用于返回数组的值(例如我的个人示例),但是当我尝试返回多维数组的值(例如使用我上面的书的例子)。请记住,我仍在尝试想办法让 __call 方法检查一个方法是否存在,如果不存在,则检查另一个方法。
请在这里给我留言。我一直把头撞在墙上试图弄清楚这个问题,这简直要了我的命。
<?php
class Test
{
protected $person;
protected $book;
public function __construct()
{
$this->person["first_name"] = "John Smith";
$this->book["genre"]["title"] = "Suspense";
}
public function __call($method, $args)
{
$args = implode(", ", $args);
if (preg_match("/^fetch_([A-Z]{1,}[a-z]{1,})(.*)?/", $method, $match))
{
print_r($match);
echo "<br><br>";
$property = strtolower($match[1]);
$indexes = $match[2];
if (property_exists("Test", $property))
{
if ($indexes)
{
$indexes = preg_split("/(?<=[a-z])(?=[A-Z])/", $indexes);
$num_indexes = count($indexes);
$count_indexes = 1;
for ($count=0; $count<$num_indexes; $count++)
{
$record = strtolower($indexes[$count]);
$index .= $record;
$array .= "{$record}";
$var_index = $index;
$var_array = $array;
echo $var_index." {$count}<br>";
echo $var_array." {$count}<br>";
//print_r($this->{$property}{$var_array});
if ($count_indexes == $num_indexes)
{
if (isset($this->{$property}{$var_index}))
{
return $this->{$property}{$var_index};
}
else
{
return $this->{$property}{$var_array};
}
}
else
{
$index .= "_";
}
$count_indexes++;
}
echo "<br><br>";
}
else
{
return $this->{$property};
}
}
}
}
}
?>
<?php
$test = new Test();
echo $test->fetch_PersonFirstName();
echo "<br><br>";
echo $test->fetch_BookGenreTitle();
?>
I've been trying to think of a way to dynamically return property values for a class using __call instead of creating a slew of functions whose only purpose would be to return those values. The idea I have is to be able to request ( for example ) $this->fetch_PersonFirstName() and have the class check if $this->person["first_name"] is set and return the value. Another example would be calling $this->fetch_BookGenreTitle() and have the class return the value of $this->book["genre"]["title"]. I know something like this would have to do a bit of checking in order for it to automatically determine that, for example, since $this->book["genre_title"] doesn't exist, then it should check for $this->book["genre"]["title"].
So far I've come up with code that ( for some reason ) works for returning the values of an array ( such as my person example ) but my problem quickly develops when I try to return the values of a multidimensional array ( such as with my above book example ). Bare in mind, I'm still trying to think of a way for the __call method to check for the existence of one, and if it doesn't exist, then the other.
Please, throw me a line here. I've been banging my head against the wall trying to figure this out and it's killing me.
<?php
class Test
{
protected $person;
protected $book;
public function __construct()
{
$this->person["first_name"] = "John Smith";
$this->book["genre"]["title"] = "Suspense";
}
public function __call($method, $args)
{
$args = implode(", ", $args);
if (preg_match("/^fetch_([A-Z]{1,}[a-z]{1,})(.*)?/", $method, $match))
{
print_r($match);
echo "<br><br>";
$property = strtolower($match[1]);
$indexes = $match[2];
if (property_exists("Test", $property))
{
if ($indexes)
{
$indexes = preg_split("/(?<=[a-z])(?=[A-Z])/", $indexes);
$num_indexes = count($indexes);
$count_indexes = 1;
for ($count=0; $count<$num_indexes; $count++)
{
$record = strtolower($indexes[$count]);
$index .= $record;
$array .= "{$record}";
$var_index = $index;
$var_array = $array;
echo $var_index." {$count}<br>";
echo $var_array." {$count}<br>";
//print_r($this->{$property}{$var_array});
if ($count_indexes == $num_indexes)
{
if (isset($this->{$property}{$var_index}))
{
return $this->{$property}{$var_index};
}
else
{
return $this->{$property}{$var_array};
}
}
else
{
$index .= "_";
}
$count_indexes++;
}
echo "<br><br>";
}
else
{
return $this->{$property};
}
}
}
}
}
?>
<?php
$test = new Test();
echo $test->fetch_PersonFirstName();
echo "<br><br>";
echo $test->fetch_BookGenreTitle();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
再次感谢各位。我想我终于有了我的解决方案,它适用于多维数组或任何深度,并考虑确定属性是否为,例如 $this->book["genre_title"] 或 $ this->book["genre"]["title"] :)
我发布下面的代码,因为其他人可能会在将来随机发现它有用
很有可能有一些方法来改进/简化代码,在这种情况下,非常欢迎任何想要这样做的人发布改进的版本。
Thanks again folks. I think I finally have my solution, which works for multidimensional arrays or any depth and accounts for determining whether a property is, for example $this->book["genre_title"] or $this->book["genre"]["title"] :)
I'm posting the code below as someone else may randomly find it useful in the future
There's more than likely some ways to improve/streamline the code, in which case anyone wanting to do so is more than welcome to post an improved version.
给定“BookGenreTitle”:
property_exists($this, "Book")
array_key_exists("genre", $ this->book)
$this->book["genre"]
array_key_exists("genre_title", $ this->book)
$this->book["genre_title"]
array_key_exists("genre", $这本书)&& array_key_exists("title", $this->book["genre"])
可能有一种方法可以使用循环或某种递归而不是硬编码最大深度,但我不会'现在就开始吧……
哦,正如另一位发帖者所说,您想要的是属性重载(
__get
和__set
)。Given "BookGenreTitle" :
property_exists($this, "Book")
array_key_exists("genre", $this->book)
$this->book["genre"]
array_key_exists("genre_title", $this->book)
$this->book["genre_title"]
array_key_exists("genre", $this->book) && array_key_exists("title", $this->book["genre"])
There's probably a way to use a loop or some sort of recursion instead of hard-coding the maximum depth, but I won't get into that now...
Oh, and as the other poster said, what you want is property overloading (
__get
and__set
).您需要查看属性重载,而不是方法重载< /a> 正如您自己已经在问题标题中弄清楚的那样。
You need to take a look at property overloading, not method overloading as you've already figured out in the question's title yourself.