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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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". Quote Link to comment 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?? Quote Link to comment 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? Quote Link to comment 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(); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.