Welcome Guest, Not a member yet? Register   Sign In
Library -Object - Hydrate


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:

class Member

protected $_MEMBER_ID,

 public function hydrate(array $donnees)
   foreach ($donnees as $key => $value)
     $method 'set'.ucfirst($key);

public function __construct(array $donnees)
$this ->Hydrate($donnees); 

public function MEMBER_ID() { return $this->_MEMBER_ID ;}
 public function 
MEMBER_EMAIL() { return $this->_MEMBER_EMAIL ;}
public function setMEMBER_ID($MEMBER_ID) { $this->_MEMBER_ID $MEMBER_ID; }
 public function 


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
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()
= new Member([   
'MEMBER_ID'              => "1",
       'MEMBER_NAME'            => $this->input->post('InputName') ,



Any help to guide me on the right path ? 

Thank you and sorry for the beginner mistakes. 


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.

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 ()
       $this->form_validation->set_rules('InputName''Name' ,'trim|required');
       $this->form_validation->set_rules('InputFirstName''First Name' ,'trim|required');        

($this->form_validation->run() == false)

PHP Code:
       function createmember()
        $member = new Member([   
'MEMBER_NAME'              => $this->input->post('InputName')
       'MEMBER_FRIST_NAME     => $this->input->post('InputFirstName') 

And in my library 'member' : 
PHP Code:
class Member
protected     $_MEMBER_ID,
 public function 

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'...

You can stick with your original idea, but...

public function hydrate(array $donnees = null)
   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.

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:

class ExampleFactory {

    protected $prototypes = [];
    protected $hydrators  = [];

    public function configure($prototype, Callable $hydrator)
        $class = get_class($prototype);
        $this->prototypes[$class] = $prototype; // This is a "bare" object
        $this->hydrators[$class]  = $hydrator;

    public function create($class, $data)
        $object = clone $this->prototypes[$class]; // The real magic is here - we don't instantiate, but copy
        call_user_func($this->hydrators[$class], $object, $data); // The hydrator should know how to populate the bare object
        return $object;

(don't use this, it's just an example)

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,

Theme © iAndrew 2016 - Forum software by © MyBB