本文介绍了PHP从类外部将函数var从false设置为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类中有一个烦人的函数,它添加了广告,我希望将它们从类外删除。该函数如下所示:

There is an annoying function inside a class which adds advertisements and I want them removed from outside the class. The function looks like this:

class Ads_Form {
    public function admin_footer( $submit = true, $show_sidebar = true ) {
        if ( $show_sidebar ) {
            $this->admin_sidebar();
        }
    }
}

您可以看到有变量 $ show_sidebar ,默认情况下设置为 true

As you can see there is a variable called $show_sidebar which is set to true by default.

如何从班级外部将此变量设置为 false

How can I set this var to false from outside the class?

推荐答案

调用此方法时,如果不提供任何参数,则默认为false:

When you call this method, if you provide no arguments, it will default to false:

$obj = new Ads_Form();
$obj->admin_footer();
// $show_sidebar gets set to true

只需传递参数,它将覆盖默认值:

Just pass it arguments, which will override the defaults:

$obj = new Ads_Form();
$obj->admin_footer(true, false);
// $show_sidebar gets set to false

这篇关于PHP从类外部将函数var从false设置为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:41
查看更多