Omirion Posted July 29, 2010 Share Posted July 29, 2010 Catchable fatal error: Object of class Person could not be converted to string in D:\Program Files\AppServ\www\Project 1\FrontSide\Spawner.php on line 8 Spawner.php <html><body> <?php require_once("../Person/Person.php"); $count = $_POST['count']; $add = 0; while ($add < $count) { $one.$add = new Person('laryy'); $add++; } $one->getInfo('age'); ?> </body></html> I don't understand what's wrong. If you need more of the code please ask Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/ Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 What are you trying to do on this line?: $one.$add = new Person('laryy'); Are you trying to use variable variables? Where is $one defined? Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092625 Share on other sites More sharing options...
Omirion Posted July 29, 2010 Author Share Posted July 29, 2010 What are you trying to do on this line?: $one.$add = new Person('laryy'); Are you trying to use variable variables? Where is $one defined? good point... Can i have a little help with this? How would the code look like. If i wanted to create as many objects as $count is. Array a better solution? Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092736 Share on other sites More sharing options...
ToonMariner Posted July 29, 2010 Share Posted July 29, 2010 it wasn't a point it was a question - you are concatenating to variables (which will make them a string) and then trying to assign an object to them.. rather than going down the variable variable route I'd suggest an array... <?php require_once("../Person/Person.php"); $count = $_POST['count']; $people = array() while ($add++ < $count) { $people[] = new Person('laryy'); } ?> however as you pass larry to the constructor I don't think this is your intention.... Rather than guessing at what you are trying to achieve please tell us. posting code without any description of what it is meant to do is often pointless (unless your code is good enough to document itself). Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092749 Share on other sites More sharing options...
Omirion Posted July 29, 2010 Author Share Posted July 29, 2010 it wasn't a point it was a question - you are concatenating to variables (which will make them a string) and then trying to assign an object to them.. rather than going down the variable variable route I'd suggest an array... <?php require_once("../Person/Person.php"); $count = $_POST['count']; $people = array() while ($add++ < $count) { $people[] = new Person('laryy'); } ?> however as you pass larry to the constructor I don't think this is your intention.... Rather than guessing at what you are trying to achieve please tell us. posting code without any description of what it is meant to do is often pointless (unless your code is good enough to document itself). The contructor of the class Person takes in a name and age. I will post it but it's sorta hectic as i am practicing OOP. Basicly so far I have a form that takes in a number. Sends the number to a handler php file that creates as many instances of the class person as the number. Later i want to have the front end show statistics of each individual Person. Here is all the code, if you have free time knock yourself out. <?php require_once('../Core.php'); require_once('Abilities/Aging.php'); class Person extends Core { protected $cTime; protected $age; protected $name; function __construct($name,$age = 0){ if (!is_numeric($age)) { throw new Exception('Age is not numeric in __Contructor of Person'); } if (empty($name)) { throw new Exception('Name is not valid in __Contructor of Person'); } $this->age = $age; $this->name = $name; $this->cTime = date('i'); } #### person::getInfo() #### Params: Info #### Result: Echoes requested info tag, if exists. #### Else returns false. public function getInfo($infoTag){ switch ($infoTag){ case 'name': echo $this->name; break; case 'age': echo $this->age; break; case 'cTime': echo 'Person was created at '.$this->cTime; break; default: throw new Exception('Info request invalid in class Person'); } } public function setInfo($infoTag,$value){ switch ($infoTag){ case 'name': if (!empty($value)) { $this->name = $value; } else { throw new Exception('Name is empty in setInfo() in class Person'); } break; case 'age': if (is_numeric($value)) { $this->age = $value; } else { throw new Exception('Age is not numeric in setInfo in class Person'); } break; default: throw new Exception('infoTag invalid in setInfo in class Person'); } } public function age(){ $ageMech = new Aging(); $temp = $ageMech->getFactor($this->cTime); $this->age = $temp; } } ?> <?php class Aging { public function getFactor($cAge){ $tempData = date('i'); $temp = $tempData - $cAge ; return $temp; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092757 Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 I'm still not sure what you're asking, and I don't see how that additional code relates to your original question. Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092765 Share on other sites More sharing options...
Omirion Posted July 29, 2010 Author Share Posted July 29, 2010 ok in short how do i dynamically create variables. That is to say $var1 $var2 $var3 $var4 .... $varN Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092838 Share on other sites More sharing options...
Alex Posted July 29, 2010 Share Posted July 29, 2010 It would probably be a better idea to use an array like in ToonMariner's solution, but if you really want to use variable variables you can do it like this: require_once("../Person/Person.php"); $count = $_POST['count']; $add = 0; while ($add < $count) { ${'var' . $add} = new Person('laryy'); $add++; } Quote Link to comment https://forums.phpfreaks.com/topic/209236-catchable-fatal-error/#findComment-1092846 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.