如何使用__set魔术方法查看指定数组NAME及处理实例中意外数组定义
__set Magic Method with Arrays Let's break down your questions with practical, easy-to-follow examples to clarify how __set works with arrays.
1. How to use __set to "view" a specified array (by its name)?
First, a quick recap: The __set($name, $value) magic method triggers whenever you try to assign a value to an inaccessible (private/protected) or undefined class property. To "view" the array—whether its name or its contents—you can directly reference the $name parameter (which holds the array/property name) and inspect the $value (the array being assigned).
Here's a hands-on example:
class ArrayManager { // Private storage to keep our arrays safe private $storedArrays = []; public function __set($name, $value) { // Verify we're dealing with an array if (is_array($value)) { // Output the array's name and its contents echo "Array name: *{$name}*\n"; echo "Array contents:\n"; print_r($value); // Store the array for later use $this->storedArrays[$name] = $value; } else { echo "Only array values are allowed here!\n"; } } } // Test it out $manager = new ArrayManager(); // Assign an array to a "NAME" property (which doesn't exist publicly) $manager->NAME = ['Alice', 'Bob', 'Charlie'];
When you run this, __set catches the assignment to $manager->NAME, prints the array's name and contents, then saves it to the private storage.
2. How to access an "unexpected" array via __set, output its name, or rename it?
If your class ends up with a dynamically assigned array you didn't plan for, __set will still trigger when the array is first assigned (since it's an undefined property). Here's how to handle these cases:
Example 1: Detect and output the unexpected array's name
Check if the array's name isn't in your predefined allowed list, then flag it:
class ArrayManager { private $storedArrays = []; // List of arrays we expect to receive private $expectedArrays = ['NAME', 'teamMembers']; public function __set($name, $value) { if (is_array($value)) { // Check if this is an unexpected array if (!in_array($name, $this->expectedArrays)) { echo "Unexpected array detected: *{$name}*\n"; } $this->storedArrays[$name] = $value; } } } $manager = new ArrayManager(); // This is an unplanned array "arrayName" $manager->arrayName = ['apple', 'banana', 'cherry'];
Example 2: Rename the submitted array
If you want to rebrand an array from its original name to a new one, map the old name to a new key inside __set:
class ArrayManager { private $storedArrays = []; public function __set($name, $value) { if (is_array($value)) { // Rename "arrayName" to "fruitCollection" if ($name === 'arrayName') { $newArrayName = 'fruitCollection'; echo "Renamed array from *{$name}* to *{$newArrayName}*\n"; $this->storedArrays[$newArrayName] = $value; } else { $this->storedArrays[$name] = $value; } } } // Add __get to retrieve the renamed array later public function __get($name) { return $this->storedArrays[$name] ?? null; } } $manager = new ArrayManager(); $manager->arrayName = ['apple', 'banana']; // Fetch the renamed array print_r($manager->fruitCollection);
This code takes the array assigned to arrayName, renames it to fruitCollection, and lets you access it with the new name using __get.
内容的提问来源于stack exchange,提问作者Thompson007

