问题标签 [magic-methods]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
php - “__class 魔术方法”(通过自定义代码调解对类名的引用)
您可以使用 __get、__call 将调用重定向到某些属性/函数。
有没有办法在课堂上做到这一点?
我想将代码中对 some_class_name 的所有提及转换为 MY_VERSION_some_class_name (不仅是一个类,它是一种模式)。
如果方法/属性是此重命名策略的目标,这将很容易。
你能想出一种方法来为 PHP 中的类做这件事吗?
编辑:我需要这个来引用不同情况下类的不同变体。我需要将一个类名解析为此类的不同变体,具体取决于运行时已知的条件,并在整个会话中持续存在。
谢谢
ps 如果您好奇我为什么要这样做,请查看维护应用程序的变体
php - 帮助使用 __call 和 codeigniter
我在 codeigniter 中有一个类,它只处理 ajax。我在此类中创建了一个函数,用于检查引用是否为 ajax 引用,我希望每次使用此类中的任何函数时都能够调用此函数。
因此,我已经实现了__call
魔术方法
目前ajaxCheck()
总是返回false
和exit()
s。但它没有被调用(目前我的 ajax 请求仍然返回数据)这是解决问题的有效方法吗?
php - 使用 __set 解决了数组,但为什么呢?
经过一番研究,我最终找到了一个我很快要在这里问的问题的答案;你如何通过 PHP 中的__get
和__set
魔法方法处理数组?每当我尝试使用类似的东西设置一个值$object->foo['bar'] = 42;
时,它似乎都会默默地丢弃它。
无论如何,答案很简单;该__get
方法只需要通过引用返回。在它前面扔了一个&符号之后,果然它起作用了。
我的问题实际上是为什么?我似乎无法理解为什么这是有效的。__get
按引用返回如何影响__set
使用多维数组?
编辑:顺便说一下,运行 PHP 5.3.1
php - 通过引用使用 __get()
使用这样的示例类:
的一个实例Test
将像这样回退输出:
有没有一种方法可以获取有关正在访问的数组的哪个维度的更多信息,向我展示(来自上一个示例)的bar
元素foo
和 的hello
元素bar
是目标?
php - 未填充类中的对象数组
所以我有这两个课程。
和
我在尝试填充 Salesman 类中的 $repdata 对象时遇到了困难......
像这样
我知道 $datatemp 已正确填充,但是当我尝试在 salesmen 对象中填充 repdata 对象时,没有显示错误,但没有填充任何内容。
我确实尝试过搜索这个,但无法完全确定我做错了什么。我熟悉 java 对象,但是 php 和“魔术”类让我头晕目眩。
任何帮助将不胜感激。
非常感谢。
php - 在 Node.js 中模仿 PHP 的 __get()、__set() 和 __call() 魔术方法
我想知道是否有办法在 Node.js 中模仿 PHP 的魔术方法 __get() 和 __set()。从这个问题:JavaScript getter for all properties I know you can do it in Rhino, but Node's built on V8. V8 有办法做到这一点吗?
php - PHP: __callStatic alternative for 5.2.x
So I have hosting that refuses to update to PHP 5.3 (which is annoying but fine at the same time; I'm flexible), but the only thing that is really ticking me off is not having __callStatic available.
I have modules loaded that I'm wanting to consolidate down to a single class (i.e. have a class that loads modules, and then accesses those modules' functions through calls to the main class' __callStatic method), but when I uploaded from a local testing server to my server, obviously the __callStatic method didn't work.
Is there an alternative that is still static? I like the clean code look for the double colon (i.e. registry::some_func()
) instead of $registry->some_func()
.
Another down-side to this is that I'm now forced to use the global
keyword within functions (which by my own preference is very, very messy).
The code was also extremely fast using static methods instead of non-static methods, and in-turn much more clean.
Also: I've thought about writing the __callStatic method as usual with the 5.2 and then calling the methods explicitly, for example registry::__callStatic("some_func"), array("val", "val2", "val3")
and keeping things simpler that way, even being easier to migrate when they do upgrade to 5.3. Thoughts on this method?
Thanks!
php - PHP __get 和 __set 魔术方法
除非我完全弄错了,否则__get
and__set
方法应该允许重载 →get
和set
.
例如,以下语句应调用该__get
方法:
以下应使用该__set
方法:
这在我的代码中不起作用,并且可以通过这个简单的示例重现:
这只会导致:
在那里打一些die()
电话表明它根本没有击中它。
现在,我只是说搞砸了,现在手动使用__get
它现在需要的地方,但这不是很动态,需要知道除非特别调用,否则实际上不会调用“重载”代码。我想知道这是否不应该按照我理解的方式运行,或者为什么这不起作用。
这是在运行php 5.3.3
。
python - Python中的对象回滚、写时复制、版本化代理等
前提:给定一个 Python 对象obj
,我想将它传递给一些随机函数,并且,当函数完成时,我需要重置obj
为原始状态的选项。此外,不能对它进行实际更改,obj
因为其他代码可能仍希望访问它的原始状态。
obj
在一个大的只是稍微修改的常见情况下,最佳解决方案应该很快。obj
需要回滚的罕见情况下的性能不太重要。
这些要求与简单地复制对象的蛮力解决方案是正交的:在常见情况下它会非常慢,而对于不常见的回滚来说却非常快。
该解决方案通常应允许处理对象的代码将其视为普通对象。这包括为其分配各种属性,包括自定义类。显然,解决方案需要考虑到整个对象树。可能需要一些让步。到目前为止,我在我的解决方案中考虑的限制示例包括要求非基本类型都继承自一个特殊的基类,不允许 dicts 和列表以换取元组和自定义 dict 类等。主要的奥秘是可以接受的。
我已经为此工作了一段时间,并且很想看看更有经验的 Python 向导是否有什么想法和建议。
编辑:Fred 的回答让我意识到了一个缺失的要求:不能对 original 进行任何更改
obj
,因为原始状态也很有价值。
python - Python 类的 __dict__.__dict__ 属性是什么?
如果我这样做A.something = 10
,这将进入A.__dict__
。这是在什么中<attribute '__dict__' of 'A' objects>
发现的A.__dict__.__dict__
,它什么时候包含一些东西?