duanbipu1720 2014-04-08 21:57
浏览 111
已采纳

Phalcon在模型中初始化函数

I have 3 models, Where

Basemodel
Model_A
Model_B

In Basemodel i have initialize function setting a property value

In Model_A i don't have initialize function, if i return anything from model 
it will be successful

In Model_B i have initialize function, if i return anything from this model 
its not returning anything. No Error, Nothing, Just Blank.

Base Models With initialize()

class Basemodel extends \Phalcon\Mvc\Model {

    protected $name;

    function initialize() {
        $this->name = 'MyApplication';
    }

}

Model A Without initialize():

class Model_a extends Basemodel {
    function test() {
        return $this->name; // WORKING
    }
}

Model B With initialize():

class Model_b extends Basemodel {
    public $b;
    function initialize() {
        $this->b = 'someName';
    }

    function rawtext() {
        return $this->name;  // NOT WORKING
    }
}

If remove initialize() from Model_B, Then its returning the value successfully

展开全部

  • 写回答

1条回答 默认 最新

  • duandou9931 2014-04-08 22:38
    关注

    This happens because in child classes you are overriding BaseModel initialize method.

    If you want to extend initilize in child class you should call initialize from parent class - here BaseModel.

    Model A/B should be:

    class Model_b extends Basemodel {
        public $b;
        function initialize() {
            parent::initialize(); // Call parent's class initialize
            $this->b = 'someName';
        }
    
        function rawtext() {
            return $this->name;  // SHOULD WORK
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部