-
martin88 Newbie

-
Posts: 5
Threads: 2
Joined: Nov 2016
Reputation:
0
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
class Member {// BEGINING OF THE CLASS
//ATTRIBUTES protected $_MEMBER_ID, $_MEMBER_EMAIL,
//HYDRATATION public function hydrate(array $donnees) { foreach ($donnees as $key => $value) { $method = 'set'.ucfirst($key); if (method_exists($this, $method)) { $this->$method($value);
} } } //CONSTRUCTEUR public function __construct(array $donnees) { $this ->Hydrate($donnees); }
//GETTERS public function MEMBER_ID() { return $this->_MEMBER_ID ;} public function MEMBER_EMAIL() { return $this->_MEMBER_EMAIL ;} //SETTERS public function setMEMBER_ID($MEMBER_ID) { $this->_MEMBER_ID = $MEMBER_ID; } public function setMEMBER_EMAIL($MEMBER_EMAIL) { $this->_MEMBER_EMAIL = $MEMBER_EMAIL; }
} // END OF THE CLASS ?>
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() { $member = 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.
M.
-
martin88 Newbie

-
Posts: 5
Threads: 2
Joined: Nov 2016
Reputation:
0
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');
if ($this->form_validation->run() == false) { $this->load->view('sign/signUp'); } else { $this->MemberModel->createmember(); } }
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 {//ATTRIBUTES protected $_MEMBER_ID, $_MEMBER_EMAIL, $_MEMBER_NAME, $_MEMBER_FIRST_NAME, $_MEMBER_BIRTH_DAY, $_MEMBER_ACCOUNT_MANAGER, $_MEMBER_CREATION_DATE, $_MEMBER_ACCOUNT_ID, $_MEMBER_ADMIN, $_MEMBER_PASS, $_MEMBER_LOG_TIME, $_MEMBER_NUM_CONNECT; public function __construct() { } }
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'...
-
ciadmin Cat Herder
      
-
Posts: 1,319
Threads: 21
Joined: Jan 2014
Reputation:
71
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.
-
martin88 Newbie

-
Posts: 5
Threads: 2
Joined: Nov 2016
Reputation:
0
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.
|