代码之家  ›  专栏  ›  技术社区  ›  Bula

Yii中php中无法识别类方法

  •  -2
  • Bula  · 技术社区  · 12 年前

    我有一个简单的模型,它添加了2个作为参数给出的数字,但是看起来内置函数add()没有被识别。

    这是模型

        <?php
    
    
    class SimpleMaths extends CModel{
    
        private $numberone;
        private $numbertwo;
    
    
        public function SimpleMaths($numberones,$numbertwos){
            $numberone = $numberones;
            $numbertwo = $numbertwos;
    
        }
    
        public function add()
        {
            return $numbertwo + $numberone;
        }
    
        public function attributeNames(){
            return array('number 1' => $numberone, 'number 2'=> $numbertwo);
        }
    }
    
    
    ?>
    

    这是控制器:

        <?php
    
    
    class BlogController extends Controller{
    
        public function actionIndex(){
            $none = $_GET['n1'];
            $ntwo = $_GET['n2'];
            $model = new SimpleMaths($ntwo,$none);
            // $sum = $model.add();
            $array = get_class_methods('SimpleMaths');
    
            $this->render("index",array("interesting"=>$array,"model"=>$model));
        }
    
    
    
    }
    
    
    
    ?>
    

    这是视图

        <?php
    
    echo "Hello World";
    foreach($interesting as $one)
    {
        echo $one."<br>";
        echo "<hr>";
        echo $model.add();
    }
    
    ?>
    

    add函数被显示为其中一个函数,但是当调用时,它只是隐藏自己?

    错误如下:

    Fatal error: Call to undefined function add() in C:\wamp\www\testdrive\protected\views\blog\index.php on line 8
    
    1 回复  |  直到 12 年前
        1
  •  2
  •   Developerium Acorn    12 年前

    在您显示的代码中 echo $model 这是一个 对象 ,然后追加任何函数 add() 返回到它,

    你需要把它改成

    echo $model->add();
    
    推荐文章