CodeIgniter Forums
User's library can't use the __construct method passing parameters??? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: User's library can't use the __construct method passing parameters??? (/showthread.php?tid=64600)



User's library can't use the __construct method passing parameters??? - leonhou - 03-10-2016

today i create a library with a method __construct ,and the construct method have two parameters ,it's like this
Class Test
{
     private $a;
     public  function __construct($b)
     {
            $this->a = $b;
      }
      public function show()
      {
            echo $this->a;
       }
 }

but when i load the this library in  my controller, it posts an error saying that the __construct method missing one parameter

so the question is that is the library in Codeigniter can't use the __construct() method to passing parameters???????

help please........


RE: User's library can't use the __construct method passing parameters??? - RobertSF - 03-10-2016

Hi, leon hou, and welcome to CodeIgniter and the forums! Smile

What you are doing is ok, but when you load the library, you must pass the argument at that time. It's missing one parameter probably because you are not sending it. When you load the library, you must use something like
$this->load->library('test', $arguments);

Check out these pages.
https://www.codeigniter.com/user_guide/general/creating_libraries.html
http://stackoverflow.com/questions/12953232/passing-parameters-when-initializing-a-library-in-codeigniter
http://stackoverflow.com/questions/3480633/passing-arguments-when-loading-custom-codeigniter-library
http://stackoverflow.com/questions/10655107/load-library-by-passing-parameter-to-constructor-in-codeigniter

I hope that helps!


RE: User's library can't use the __construct method passing parameters??? - leonhou - 03-17-2016

(03-10-2016, 10:33 PM)RobertSF Wrote: Hi, leon hou, and welcome to CodeIgniter and the forums! Smile

What you are doing is ok, but when you load the library, you must pass the argument at that time. It's missing one parameter probably because you are not sending it. When you load the library, you must use something like
$this->load->library('test', $arguments);

Check out these pages.
https://www.codeigniter.com/user_guide/general/creating_libraries.html
http://stackoverflow.com/questions/12953232/passing-parameters-when-initializing-a-library-in-codeigniter
http://stackoverflow.com/questions/3480633/passing-arguments-when-loading-custom-codeigniter-library
http://stackoverflow.com/questions/10655107/load-library-by-passing-parameter-to-constructor-in-codeigniter

I hope that helps!
thank you very much ,get that , i am using the wampserver integrating a debugger.......