非静态不称为静态

时间:2016-09-26 20:40:01

标签: php static call

如何动态调用该方法? 我称这个功能为:

    public function type_bien($value)
    {
        $stype['1'] = "Maison";
        $stype['2'] = "Appartement";
        $stype['4'] = "Appartement meublé";
        $stype['7'] = "Propriété";
        return $stype[$value];
    }

<?php echo type_bien($row['type_bien']); ?>

-> Returns: Deprecated: Non-static method Utils::type_bien() should not be called statically

2 个答案:

答案 0 :(得分:0)

如果您尝试动态调用方法,请检查以下示例:

<?php

Class Demo {
    public function test () {
      echo "OK";
    }
}

$demo_1= new Demo();
$dynamic = "test";

$demo_1->{$dynamic}();

// i.e. $demo_1->test();
// prints OK

// or

Demo::{$dynamic}();
// i.e. Demo::test();
// prints OK

?>

答案 1 :(得分:0)

您可以创建Utils类的对象

class Utils {
 public function type_bien($value) {
    $stype['1'] = "Maison";
    $stype['2'] = "Appartement";
    $stype['4'] = "Appartement meubl&eacute;";
    $stype['7'] = "Propri&eacute;t&eacute;";
    return $stype[$value];
 }
}

$utils = new Utils();
echo $utils->type_bien($row['type_bien']);

或者您可以将方法定义为静态函数

class Utils {
 public static function type_bien($value) {
    $stype['1'] = "Maison";
    $stype['2'] = "Appartement";
    $stype['4'] = "Appartement meubl&eacute;";
    $stype['7'] = "Propri&eacute;t&eacute;";
    return $stype[$value];
 }
}

echo Utils::type_bien($row['type_bien']);