Welcome Guest, Not a member yet? Register   Sign In
Class instantiation issue with CI 3.0.2 & PHP 5.6
#1

I just upgraded an existing app to CI v3.02 & PHP v5.6.  I'm running into a conflict when instantiating a class then accessing certain methods in $this var, the methods seem to be overwritten by the new class instance.  Here is an example, imagine the first line is being called from some other class:



PHP Code:
$upload = (new Post)->uploadImage($data);


class 
Post extends Main {

 
    public function __construct(){parent::__construct();}

 
    public function uploadImage($data){

 
         ...

 
         $upload_dir $this->config->item('upload_dir');

 
         $this->load->library('upload'$config);
 
         $upload_result $this->upload->do_upload($field);

 
         ...

 
    }




$this->config is returning the expected value I have in my config file.

$this->upload is throwing the error "Undefined property: Post::$upload" which means it has overwritten the upload library in CodeIgniter.  How can I avoid this from happening?

A few notes:
  1. This was not happening in the previous code I had when instantiating the class by using Post::uploadImage(...)
  2. There is no upload() method in the Post class, nor is there code that resets $this->upload
  3. If I add $CI =& get_instance(); then replace $this with $CI, I get the same issue.

I'd appreciate any help.  Thank you.
Reply
#2

Is CI_Controller an ancestor of that class? Controllers in CI are singletons, you're not supposed to instantiate more than one.
Reply
#3

(10-20-2015, 01:39 AM)Narf Wrote: Is CI_Controller an ancestor of that class? Controllers in CI are singletons, you're not supposed to instantiate more than one.

Yes, CI_Controller was an ancestor of the Post class.  Thanks for pointing out that they're singletons.  I corrected the issue by eliminating multiple calls to the controllers.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB