Guest Xanza Posted June 9, 2008 Share Posted June 9, 2008 I usually do this kinda stuff where I find something interesting about PHP, and try to replicate it... And recently I got into here-doc - and I came up with this just to test it out and play with it: <?php class info{ var $name; var $age; var $group; function info(){ $this->name = "Hello, my name is {$name}, "; $this->age = "and I am {$age} year's old"; $this->group = array("1","2","3","4","5"); } } $info = new info(); echo <<<EOT {$info->name}{$info->age}, and am in group {$info->group[1]}! EOT; ?> Then I friggin' realized that I know nothing about classes really... lol! I've been reading up in the php docs but I can't really find a solution to my question, which would be what's the easiest way to add an array to the other two variables that I have in this script: "name" and "age". Any help would be appreciated! Thanks again, Xanza Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/ Share on other sites More sharing options...
hansford Posted June 9, 2008 Share Posted June 9, 2008 class info{ var $name = array(); var $age = array(); var $group; Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-560971 Share on other sites More sharing options...
Guest Xanza Posted June 9, 2008 Share Posted June 9, 2008 That's what I originally had, but unfortunately it dident work.. Any specific reason why? <?php class info{ var $name = array("Jeff","Ducky","Zach"); var $age = array("18","19","20"); var $group; function info(){ $this->name = "Hello, my name is {$name}, "; $this->age = "and I am {$age} year's old"; $this->group = array("1","2","3","4","5"); } } $info = new info(); echo <<<EOT {$info->name[0]}{$info->age[0]}, and am in group {$info->group[1]}! EOT; ?> And this outputs: http://xanza.info/test.php Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-560975 Share on other sites More sharing options...
hansford Posted June 9, 2008 Share Posted June 9, 2008 function info($num){ $name = "Hello, my name is " . $this->$name[$num]; $age = "and I am " . $this->$name[$num] . " year's old"; $this->group = array("1","2","3","4","5"); } Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-560986 Share on other sites More sharing options...
Darklink Posted June 9, 2008 Share Posted June 9, 2008 Hmm... it seems your objective here is very strange. You are trying to instanitiate the object with an initiation function that messes with the variables, yet you want to echo a value from an array of which you have changed in the past. Your best idea would be to do the following: <?php class info { var $name = array ("Jeff", "Ducky", "Zach"); var $age = array ("18", "19", "20"); var $group = array ("1", "2", "3", "4", "5"); function info($num) { $this->name = "Hello, my name is ". $this->name[$num] .", "; $this->age = "and I am ". $this->age[$num] ." year's old"; $this->group = $this->group[$num]; } } $info = array(); $info[0] = new info(0); // Object 1 $info[1] = new info(1); // Object 2 echo <<<EOT {$info[0]->name}{$info[0]->age}, and am in group {$info[0]->group}! EOT; ?> Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-561115 Share on other sites More sharing options...
craygo Posted June 9, 2008 Share Posted June 9, 2008 You are kind of missing the point of a class. Classes allow you to use multiple functions and variables without having to set global values and keep passing variables from function to function. <?php class info{ var $name; var $age; var $group = array(); function info(){ $part[] = "Hello, my name is ".$this->name.", "; $part[] = "and I am ".$this->age." year's old"; $part[] = $this->group; return $part; } } // call the class $info = new info(); // Set your variables $info->name = "John"; $info->age = "32"; $info->group = array(1,2,3,4,5); // assign a variable to your class function $part = $info->info(); echo <<<EOT {$part[0]}{$part[1]}, and am in group {$part[2][1]}! EOT; ?> Ray Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-561128 Share on other sites More sharing options...
Guest Xanza Posted June 9, 2008 Share Posted June 9, 2008 Thanks a ton guys, I'm learning tons about classes just from this thread. Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-561501 Share on other sites More sharing options...
DarkWater Posted June 9, 2008 Share Posted June 9, 2008 If you're using PHP 5, stop using the deprecated syntax that you're currently using. Use visibility keywords and use the proper constructors and stuff. (Sorry, I'm pretty strict when I do OOP. Damned Java and C++. D: ) Quote Link to comment https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/#findComment-561504 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.