duanpo1498 2014-03-28 00:54
浏览 36
已采纳

扩展类不会看到父Codeigniter的属性

I call a CodeIgniter controller method -imgupload- from jquery ajax. This controller extends my custom front controller.

class newad extends My_Controller{

public function __construct() {
    parent::__construct();
}
public function imageupload() {
    $this->load->library("uploadhandler"); 
}

The imgupload method calls the uploadhandler class which extends from newad.

class uploadhandler extends newad {

The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected. The inheritance chain looks like this: My_Controller->newad->uploadhandler.

Any idea why I cant access those properties?

  • 写回答

1条回答 默认 最新

  • drby30217 2014-03-29 01:50
    关注

    In short the answer is you do not need to extend Controller class here. You can just pass the value to your library as a parameter.

    $params = array('user_ud' => $this->userID, 'otehr' => 'other');    
    $this->load->library('uploadhandler', $params);
    
    //Now from your library
    class uploadhandler{
    
        public function __construct($params)
        {
            // Do something with $params
        }
    
       //.. Your code...//
    }
    

    Now about your question:

    The functionality of that class works properly, except for one thing, I cant access the properties of the My_Controller class, even though they are declared protected. The inheritance chain looks like this: My_Controller->newad->uploadhandler. Any idea why I cant access those properties?

    As inheritance chain are ok, you can access property of My_Controller from your library but not the value of the Current controller, because these two are different object.

    So here is my answer how can we access the value? One way I have already mentioned. That will be enough if you need to share some property with the library. But what if you need to access all the Controller instance. There is a function to get the reference of controller instance get_instance(). You can use this function anywhere and get access of all public property controller. If you need to access any private property of controller the define a geter function to access that.


    Explanation

    First of all you need to learn basic about OOP. Learn about Class, Object, Inheritance..

    When I said property of My_controller is different from the same property the you accessed from uploadhandler, it may confused you if you are not familiar with class and object. Here is two instance(object) of different class. For short let say you have some classes like: Vehicle, Car, Machine and Person. All they have common attributes say name, weight ..

    So, can we just inherit Any of these class from other??

    Simple answer is no. We can't(!) define a Person class extending from Others. So how can we decide which incoherence would legal. If you can say Foo is a Bar you can write Foo class extending from Bar. Now from your case, It is obvious uploadhandler is not a controller. So Never Extend a anything from something that is not something.

    NB: The answer is generic. If you need any specific clarification, just ask, I can update my answer

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b