jimmyt1988 Posted October 21, 2010 Share Posted October 21, 2010 why is this not working? <?php class registration{ public $email = $_POST["email"]; public $username = $_POST["username"]; public $password = $_POST["password"]; //var $username = $_POST["confirmPassword"]; function validateFields(){ echo $this->$email; echo $this->$username; echo $this->$password; //echo $this->$confirmPassword; } } registration->validateFields(); ?> Im getting error: Parse error: syntax error, unexpected T_VARIABLE on the first property declaration $email. Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/ Share on other sites More sharing options...
jimmyt1988 Posted October 21, 2010 Author Share Posted October 21, 2010 It works with strings, so im guessing that parameters cant contain functions. because $_POST is a function, i must declare each one as a public function. agh, its nothing like javascript objects lol. Any nudge in the right direction would be ACE. EDIT: Ok so i've ended up with this: <?php class registration{ public function email(){echo $_POST["email"];} public function username(){echo $_POST["username"];} public function password(){echo $_POST["password"];} public function validateFields(){ registration::email(); registration::username(); registration::password(); } } registration::validateFields(); ?> Which works... But my problem is, I kinda wanted to batch these variables(parameters) into a class so i could call them from my various method i would have. Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125022 Share on other sites More sharing options...
jimmyt1988 Posted October 21, 2010 Author Share Posted October 21, 2010 I give up. What is the point in PHP classes. I very much want to know. Ill have a break and a coffee then come back Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125027 Share on other sites More sharing options...
BlueSkyIS Posted October 21, 2010 Share Posted October 21, 2010 The larger question would be: What is the point in object-oriented programming? Here is one answer: http://wiki.answers.com/Q/Benefits_of_object_oriented_programming Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125029 Share on other sites More sharing options...
jimmyt1988 Posted October 21, 2010 Author Share Posted October 21, 2010 Ok im back. So i use Json all the time, i love it. very helpful, structured. I need a like for like in terms of how to use it in my example. I want to declare properties, username, password, email etc. then use them in a method. Any examples u can push into the forum so i can learn from them? Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125036 Share on other sites More sharing options...
BlueSkyIS Posted October 21, 2010 Share Posted October 21, 2010 me personally? no. I rarely use OOP, except where an existing class fits my needs. I was just trying to answer your "why". Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125037 Share on other sites More sharing options...
jimmyt1988 Posted October 22, 2010 Author Share Posted October 22, 2010 <?php class registration{ public $fields = array("email", "username", "password"); function validateFields(){ for ($i = 0; $i <= count($this->$fields); $i++){ echo $_POST[$this->$fields[$i]]; } } } registration::validateFields(); ?> Fatal error: Using $this when not in object context???? HUH?? Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125043 Share on other sites More sharing options...
jimmyt1988 Posted October 22, 2010 Author Share Posted October 22, 2010 hurrah, I managed to get my variables echo'd on screen, alas i still have a few notices i dont quite understand. any help? <?php class registration{ public $fields = array("email", "username", "password"); function validateFields(){ for ($i = 0; $i <= count($this->fields); $i++){ echo $_POST[$this->fields[$i]]; } } } $obj = new registration(); $obj->validateFields(); ?> Notice: Undefined offset: 3 in ** on line 11 Notice: Undefined index: in ** on line 11 how do i resolve these notices? Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125045 Share on other sites More sharing options...
jimmyt1988 Posted October 22, 2010 Author Share Posted October 22, 2010 HU-BLOOMIN-RAY YEHAA, got it working. so stupid. so so stupid. <?php class registration{ public $fields = array("email", "username", "password"); function validateFields(){ for ($i = 0; $i < count($this->fields); $i++){ echo $_POST[$this->fields[$i]] . "<br />"; } } } $obj = new registration(); $obj->validateFields(); ?> Link to comment https://forums.phpfreaks.com/topic/216514-i-dont-understand-these-object-class-thingies/#findComment-1125050 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.