从PHP对象内部或外部运行(Running from inside or outside a PHP object)

我正在使用overflow __set和__get方法,我希望能够模拟私有/公共类变量。

我相信这个问题的答案已经存在,但我一直在挖掘,找不到任何东西。 让我试着展示一下我正在尝试做的事情的一个例子。

<?php class Person { public function test() { return $this->whereami(); } public function whereami() { if (method_called_inside_class()) { return 'private'; } else { return 'public'; } } } $person = new Person(); $person->test(); // 'private' $person->whereami(); // 'public'

我想要的是在对象内调用而不是从外部调用时方法的不同功能。 我知道我可以添加另一个参数来表示行为的变化,或者创建另一个函数。 但是,如果我在这里尝试做的事情在某种程度上可能而不是其他两个选项,那将是非常棒的!

I'm using overflow __set and __get methods and I would like to be able to emulate private / public class variables.

I am sure the answer for this is already out there, but I've been digging and can't find a thing. Let me just try to show an example of along the lines of what I'm trying to do.

<?php class Person { public function test() { return $this->whereami(); } public function whereami() { if (method_called_inside_class()) { return 'private'; } else { return 'public'; } } } $person = new Person(); $person->test(); // 'private' $person->whereami(); // 'public'

What I would like is a different functionality for a method when called within an object vs. being called from outside. I know I can just add a another argument to signal a change in behavior, or create another function. But if what I'm trying to do here is in some way possible instead of those other two options, that would be awesome!

最满意答案

如果我理解正确,您希望使用未在代码中明确定义但可以使用的对象变量。 而对于他们中的一些人来说,魔法吸气剂和设定者应该返回变量,而对于其他人则不应该。

假设这样,您可以通过执行以下操作来部分解决问题。 在课堂内你必须直接用$this->data['name'];访问变量$this->data['name']; 。 我知道这不是你想要的100%,但我认为你不能真正区分课堂内和课堂外的电话。 您使用的功能甚至不存在。

<?php class MagicGetterSetter { // contains the variables private $data = array(); private $publicVariables = array('test', 'foo', 'hierarchy'); public function __get($name) { if (isset($this->data[$name])) { if (in_array($name, $this->publicVariables)) { return $this->data[$name]; } } return null; } public function __set($name, $value) { if (in_array($name, $this->publicVariables)) { $this->data[$name] = $value; } } public function __isset($name) { if (in_array($name, $this->publicVariables)) { return isset($this->data[$name]); } return false; } }

If I understand it right, you want to use object variables that are not explicitly defined in the code but can be used. And for some of them the magic getter and setter shall return the variable and for others they should not.

Assuming this, you could solve the problem partially by doing the following thing. Inside the class you would have to access the variables directly with $this->data['name'];. I know that it is not 100% of what you are looking for but I don't think you can actually differentiate between a call from within the class and from outside the class. The function you used doesn't even exist.

<?php class MagicGetterSetter { // contains the variables private $data = array(); private $publicVariables = array('test', 'foo', 'hierarchy'); public function __get($name) { if (isset($this->data[$name])) { if (in_array($name, $this->publicVariables)) { return $this->data[$name]; } } return null; } public function __set($name, $value) { if (in_array($name, $this->publicVariables)) { $this->data[$name] = $value; } } public function __isset($name) { if (in_array($name, $this->publicVariables)) { return isset($this->data[$name]); } return false; } }

更多推荐