通过父方法终止执行
我有以下代码:
class A
{
function example($bool)
{
echo "Bob";
if($bool === true)
{
//how to terminate?
}
}
}
class B extends A
{
function example($bool)
{
echo "Alice";
parent::example($bool);
echo "Charlie";
}
}
如果我打电话,
$x = new B;
$x->example(false);
我将按预期得到“AliceBobCharlie”
,我想要的是,如果我传递 true,则只会出现“AliceBob”。说:我想终止父方法中 $x->example 的执行,
希望你明白我的意思。怎么办?
感谢您的帮助
I have the following code:
class A
{
function example($bool)
{
echo "Bob";
if($bool === true)
{
//how to terminate?
}
}
}
class B extends A
{
function example($bool)
{
echo "Alice";
parent::example($bool);
echo "Charlie";
}
}
if i call
$x = new B;
$x->example(false);
i´ll get "AliceBobCharlie" as expected
and what i want is that if i pass true only "AliceBob" will appear. say: i want to terminate the execution of $x->example in the parent-method
i hope you got me. how to do this?
thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧...要么从
A::example()
返回一个布尔值,然后在B::example()
中检查它来决定是否应该继续。如果你确实想要中断,可以抛出异常。然而,使用异常来处理控制流是糟糕的设计。
Well...either you return a boolean from
A::example()
and check it inB::example()
to decide if you should continue.If you really want to have an interrupt, you can throw an exception. Using exceptions to handle control-flow is bad design however.