![]() |
Library -Object - Hydrate - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Library -Object - Hydrate (/showthread.php?tid=66629) |
Library -Object - Hydrate - martin88 - 11-14-2016 Hello, I'm trying to understand a logic but I'm maybe far far away... In my previous tutorials I learned to create an object by using a specific class. Something like : PHP Code: <?php I've tried to create that class in the library and autoload it. I have this error : An uncaught Exception was encountered Type: TypeError Message: Argument 1 passed to Member::__construct() must be of the type array, none given, called in /Applications/MAMP/htdocs/WWW/Clickandsend/system/core/Loader.php on line 1292 Filename: /Applications/MAMP/htdocs/WWW/Clickandsend/application/libraries/Member.php Line Number: 35 Backtrace: File: /Applications/MAMP/htdocs/WWW/Clickandsend/application/controllers/SignUpController.php Line: 7 Function: __construct File: /Applications/MAMP/htdocs/WWW/Clickandsend/index.php Line: 315 Function: require_once Maybe I'm wrong to try to 'hydrate' my object and I need to make it another way. I've tried to remove the construct function and create in the MemberModel and call it from the controller where I have the form validation. PHP Code: function createmember() Any help to guide me on the right path ? Thank you and sorry for the beginner mistakes. M. RE: Library -Object - Hydrate - ciadmin - 11-14-2016 Hmm, "hydrate" is a term used in one of the other frameworks, not a CI term. It looks like the intent is to populate an object, and there is nothing wrong with that. Your problem stems from the constructor declaration, with an array parameter but no default. When you "load" a model, one of them is instantiated. With no parameters to the constructor. Oops. $donnees is null, and your Hydrate method blows up. I think you need a default, like public function __construct(array $donnees = null), and that you should not try to "hydrate" anything if the parameter passed is null. RE: Library -Object - Hydrate - martin88 - 11-14-2016 That's a part of the answer. I was sure my old logic was wrong. No hydrate then. But how do I 'intent' or 'populate' my object then. I have my controller with my form validation. - InputName - InputFirstname If validation true then go to my call 'member' and use the function create member : PHP Code: public function CheckSignUp () PHP Code: function createmember() And in my library 'member' : PHP Code: class Member I still miss something right ? when I do a var_dump of my $member all the value are set to NULL. this is why I wanted a 'hydrate'... RE: Library -Object - Hydrate - ciadmin - 11-14-2016 You can stick with your original idea, but... public function hydrate(array $donnees = null) { parent::__construct(); if ($donnees != null) foreach ($donnees as $key => $value) { The intent of the above is to populate a Member object if one is being created, but to do nothing if the class is being instantiated "just because", as the CI loader does. btw, simply instantiating a model object, with an array of implied properties as a parameter, does nothing special in CI. Other frameworks might populate an object's properties under the same circumstance. RE: Library -Object - Hydrate - Narf - 11-15-2016 Not really answering the question here, but one use case I have for "hydrating" is when you'd want to create a number of objects of the same type, and you want to eliminate the instantiation cost (most likely because the constructor does some heavy operations). For example: Code: class ExampleFactory { (don't use this, it's just an example) RE: Library -Object - Hydrate - martin88 - 11-16-2016 Thank you ! you guys save my day. It works then and I'll be able to continue. I'm beginner so I'm going in lots of different ways. It seems my way of thinking is not that wright. I'm trying to learn PHP by creating my own application. The idea is to create to member pages. The member will be able to create some tasks into the database depending some projects. My idea is to have one class as explained for : - Member - Task - Project Using this way I'll be able to create a session based on the Member object. I'll be able to easily get information from my member during all the session and for creating all the other objects. Is my logic right ? Do you have any docs or examples to share in order to construct the basic correctly ? I know I'm way before the start already but I keep going and try to understand it! Not an easy part. Thank you, M. |