Jump to content

Catchable fatal error


Omirion

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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;
}

}
?>

Link to comment
Share on other sites

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++;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.