出于好奇,有没有办法在魔法get方法中传递一个数组。它必须只是魔法获取方法,或类似的东西。 (但它不应该使用其他功能去)
例如,这是可能的吗?
<?php
// Book.php
Class Book{
/**
* Hold the book's details
* @var array
*/
private $book_details = [];
function __set($name, $value)
{
$book_list[$name] = $value;
}
function __get($name)
{
if (is_array($name)) {
$collect_info = [];
foreach ($this->book_details as $item => $value) {
if ($item == $name) {
$collect_info[$item] = $value;
}
}
return $collect_info;
} else {
return $this->book_details[$name];
}
}
}
以及 resourses.php
等其他文件<?php
// resources.php
require_once "Book.php";
$book = new Book();
$book->name = "Somebook's Name";
$book->author = "This awesome dude";
$book->pages = "723";
$book->about = "Programming Content";
$book->name; // prints Somebook's Name
print_r($book->(["name", "pages"]));
// prints
// Array(
// [name] => Somebook's Name,
// [pages] => 723
// )