Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 避免调用成员函数的更好方法_Php - Fatal编程技术网

Php 避免调用成员函数的更好方法

Php 避免调用成员函数的更好方法,php,Php,我需要知道是否有更好的方法来避免在null上调用成员函数xxxx 目前,我的代码如下,但它是繁琐的 if($event->getForm() && $event->getForm()->getParent() && $event->getForm()->getParent()->getParent() && $even

我需要知道是否有更好的方法来避免在null上调用成员函数xxxx

目前,我的代码如下,但它是繁琐的

if($event->getForm()
                && $event->getForm()->getParent()
                && $event->getForm()->getParent()->getParent()
                && $event->getForm()->getParent()->getParent()->getData()
                && $event->getForm()->getParent()->getParent()->getData()->getComponente()
            ){
                $componente = $event->getForm()->getParent()->getParent()->getData()->getComponente();
                $formModifier($event->getForm(), $componente, $defaultComponente);
            }

不,没有办法,但至少你可以做一些性能改进

$form = $event->getForm();

if(!$form){
    //do error handling
    return;
}
$parent = $form->getParent();

if(!$parent){
    //do error handling
    return;
}

$p_parent = $parent->getParent();

if(!$p_parent){
    //do error handling
    return;
}

$data = $p_parent->getData();

if(!$data){
    //do error handling
    return;
}

$component = $data->getComponente();
...

这样,每个函数只调用一次,就可以更好地处理错误。不,没有办法,但至少可以提高性能

$form = $event->getForm();

if(!$form){
    //do error handling
    return;
}
$parent = $form->getParent();

if(!$parent){
    //do error handling
    return;
}

$p_parent = $parent->getParent();

if(!$p_parent){
    //do error handling
    return;
}

$data = $p_parent->getData();

if(!$data){
    //do error handling
    return;
}

$component = $data->getComponente();
...

这样,每个函数只调用一次,就可以在PHP 7中更好地处理错误。如果使用hhvm,这实际上是一个可捕获的错误。这是一个常见的异常:

try {
    $componente = $event->getForm()->getParent()->getParent()->getData()->getComponente();
} catch (\Error $e) {
    $componente = null;
}

if ($componente !== null) {
    $formModifier($event->getForm(), $componente, $defaultComponente);
}
在PHP5中,使用中间变量和and关键字(而不是&&)有一种变通方法:

如果使用&&而不是,则会收到未定义的变量通知,而此解决方案将无法工作


工作示例:

在PHP7中,如果使用hhvm,这实际上是一个可捕获的错误,这是一个常见的异常:

try {
    $componente = $event->getForm()->getParent()->getParent()->getData()->getComponente();
} catch (\Error $e) {
    $componente = null;
}

if ($componente !== null) {
    $formModifier($event->getForm(), $componente, $defaultComponente);
}
在PHP5中,使用中间变量和and关键字(而不是&&)有一种变通方法:

如果使用&&而不是,则会收到未定义的变量通知,而此解决方案将无法工作


工作示例:

我认为这是一个很好的坏代码示例。有了这样的代码,你就违反了几条规则,让你的生活变得更加艰难。 您的代码僵化、脆弱、难以理解和维护等

越简单越好


如果没有这种难看的嵌套关系,您无法使$xx->getComponent成为一个合适的对象,那么您至少应该将该方法封装到合适的对象中,并使用它,因此如果有任何变化,你不必全神贯注地到处修改它。

我认为这是一个很好的坏代码示例。有了这样的代码,你就违反了几条规则,让你的生活变得更加艰难。 您的代码僵化、脆弱、难以理解和维护等

越简单越好


如果没有这种难看的嵌套关系,您无法使$xx->getComponent成为一个合适的对象,那么您至少应该将该方法封装成合适的对象,并使用它,这样,如果有任何变化,您就不必全神贯注地改变它。

这个类在创建时似乎很奇怪,但如果不使用uu调用动态提取这些方法,则可以使用函数内循环中存在的方法,类似于:

function getMethodChain($class,$arr = ['getForm','getParent','getParent','getData','getComponente'])
{
    # First check the object is set
    if(!is_object($class))
        return false;
    # Loop intended method chain
    foreach($arr as $method) {
        # Check if the method exists in the current class or passed already
        $useClass   =   (!isset($classPass))? $class : $classPass;
        # Check if the method exists in the current class
        if(is_object($useClass) && method_exists($useClass,$method)) {
            # Assign this class/method to use next in the loop
            $classPass  =   $useClass->{$method}();
        }
        else
            return false;
    }
    # Just send back
    return (isset($classPass))? $classPass : false;
}
其用途如下:

# This will either be the data you expect or false
$componente = getMethodChain($event);

此类在创建过程中似乎很奇怪,但如果您不使用_调用动态提取这些方法,则可以使用函数内循环中存在的方法,类似于:

function getMethodChain($class,$arr = ['getForm','getParent','getParent','getData','getComponente'])
{
    # First check the object is set
    if(!is_object($class))
        return false;
    # Loop intended method chain
    foreach($arr as $method) {
        # Check if the method exists in the current class or passed already
        $useClass   =   (!isset($classPass))? $class : $classPass;
        # Check if the method exists in the current class
        if(is_object($useClass) && method_exists($useClass,$method)) {
            # Assign this class/method to use next in the loop
            $classPass  =   $useClass->{$method}();
        }
        else
            return false;
    }
    # Just send back
    return (isset($classPass))? $classPass : false;
}
其用途如下:

# This will either be the data you expect or false
$componente = getMethodChain($event);

将结果保存在变量中,并使用顺序ifs执行此操作,这样每个函数只调用一次,代码的可读性更高。将结果保存在变量中,并使用顺序ifs执行此操作,这样每个函数只调用一次,而且代码更具可读性。当你已经有了一个try-catch,并且可以在catch中插入|错误,提供相同的消息可以使用等等时,这是非常好的。否则try-catch可能会因为只是一个if而被过度使用。很高兴知道,谢谢!当您已经有了一个try-catch,并且可以在catch中插入错误,提供相同的消息,这样做非常好。否则try-catch可能会因为只是一个if而被过度使用。很高兴知道,谢谢!