- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有时我需要执行祖 parent 方法(即绕过父方法),我知道这是代码味道,但有时我无法更改其他类(框架、库等)。
在 PHP 中,我们可以通过以下方式做到这一点:
call_user_func(array(get_parent_class(get_parent_class($childObject)), 'grandParentMethod'));
问题是,如果您启用了 E_STRICT 错误,您将收到如下错误:
严格标准:非静态方法 GrandParent::grandParentMethod() 不应在...中静态调用
我只找到了一种解决方案(不删除 E_STRICT),它只是添加 @
来抑制错误。
但这真的很难看,有人知道更好的解决方案吗?
谢谢!PS:我无法实例化一个新对象,例如:
$grandparent = get_parent_class(get_parent_class($son));
$gp= new $grandparent;
$gp->grandParentMethod
因为我需要在 $son 的上下文中调用我的祖 parent 方法。
最佳答案
您可以直接通过名称调用祖 parent (不需要反射,也不需要call_user_func
)。
class Base {
protected function getFoo() {
return 'Base';
}
}
class Child extends Base {
protected function getFoo() {
return parent::getFoo() . ' Child';
}
}
class Grandchild extends Child {
protected function getFoo() {
return Base::getFoo() . ' Grandchild';
}
}
Base::getFoo
调用可能看起来像静态调用(由于冒号 ::
语法),但事实并非如此。就像 parent::
也不是静态的一样。
从类内的继承链调用方法将正确绑定(bind) $this
值,将其作为常规方法调用,遵守可见性规则(例如 protected),并且不违反任何类型!
乍一看这可能有点奇怪,但是,这是在 PHP 中实现的方法。
关于php - 如何调用祖 parent 方法而不出现 E_STRICT 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850802/
我正在动态创建一个 ContextMenu,其中包含一个 ComboxBox 作为项目之一,它由两个单独的 Canvas 使用。组合框包含百分比值,因此想法是右键单击其中一个 Canvas ,选择百分
我有一个脚本,当我指定c:\ script \ 19 \的确切目录时会运行,问题是,c:\ script中有其他文件夹,例如18、17、16。我在该脚本后附加了19所有文件的最前面。我如何看待正在重命
假设我有以下内容 每当单击 f 时,我想隐藏
我对理解 React 如何将函数作为 props 传递给子组件有一些疑问。我已经看过一些教程,但目前没有理解我的问题。 基本上我有一个简单的组件向下传递一个列表,另一个组件使用 Array.map 处
我有关注对象 task_a >> task_b>> task_c task_b 有all_done 触发规则 task_c 有all_success 触发规则 如果任务 a 失败,task_c 会被执
在层次结构的事务级别,我需要访问#grid数据中的值。这可以作为参数传递还是必须使用 jQuery 遍历 DOM? $(document).ready(function () { var el
嗨,我有如下三个类,A 类是父类,B 类是 A 的子类并且其中没有任何内容,C 类是 B 的子类和具有主要功能的测试类: public class A { double pay =0.0;
我在一个 div 中有多个 div。我希望父 div 的第一个 h1 带有下划线。first-child 的问题在于它查看父 div,在我的例子中它是错误的父 div。例如:
一个组织有很多用户 schema "organizations" do field :name, :string has_many :users, TestApp.User end 一个用户有
为什么这里匿名内部类不会被释放,导致内存泄漏? FX 2.2.1 会出现这种情况。 anchorPane.getParent().getParent().lookup("#grandParentBut
完整问题: 子表通过外键(引用祖父主键)链接到其祖父表中的行,是否可以在子表的复合外键中使用引用的祖父行中的字段? 我当前的数据库设计有一个主父表,Projects。 Projects 有两个子表,J
这可能只能在 JS 中实现。我正在为页面进行响应式设计。仅当最大宽度为 768px 时,我才需要一个子元素与其父元素具有相同的背景图像。需要注意的是,父级的背景图像是由 JavaScript 根据用户
这可能只能在 JS 中实现。我正在为页面进行响应式设计。仅当最大宽度为 768px 时,我才需要一个子元素与其父元素具有相同的背景图像。需要注意的是,父级的背景图像是由 JavaScript 根据用户
我有一个概念证明,似乎可行,但是我的一部分想知道这是否真的是一个好主意,或者是否存在使用Redux或替代策略之类的更好的解决方案。 问题 基本上,我的整个应用程序都有一个基本的React组件,其中包含
我有一个“GrandParentViewController”,它包含我的应用程序的所有方法。我有一个 subview “ParentViewController”,其中包含一些 subview “C
给定一个带有显示 flexbox .wrapper 的 div(运行并查看下面的代码片段)和一个深度嵌套的 div .text-container,我如何才能将深度嵌套的 div 居中相对于 .wra
标题听起来很困惑,但很难表达! 如何在 .feature-inner div 中居中对齐 a?每当我使用通常的 margin: 0 auto; display: block; 它只是将链接拉伸(str
我有一个名为 Device 的对象。 Device 可以有一个父 Device。一个 Device 也可以有 n 个子 Devices。 我有一个下拉列表,其中显示了所有可选择的设备。我可以很容易地获
我是 LINQ 的新手,我已经能够编写一些简单的语句。但是现在我有一个更复杂的情况,我无法弄清楚。 基本上,我正在尝试编写 LINQ to Objects 语句,其中关系是祖 parent 、 par
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Why is super.super.method(); not allowed in Java? 我有 3 个类,
我是一名优秀的程序员,十分优秀!