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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算