Jump to content
xisto Community
Sign in to follow this  
sonesay

Php Objects: Catchable Fatal Error There is a weird problem with objects in php 5.2.2

Recommended Posts

in php 4.4.7 my code seems to be ok but in later version 5.2.2 i get this error

Catchable fatal error: Object of class mission_rank could not be converted to string in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/cn_reload.php on line 41

cn_reload.php

<?phpinclude('../class/userClass.php');session_start();$app_user =& $_SESSION['app_user'];if ($app_user->character != false) {			$race_letter = array('H','E','T','M','G');			$cn_id = 0;			foreach ($app_user->character as $character) {								// div container				echo "<div class='char_node'>";				echo "<input type='hidden' name='char_node' value='$character->name'/>";					echo "<div class='char_col1'>";										//echo $app_user->character->name;					echo "$character->name <br />";					$img = "images/faces/80px-";										$race = $race_letter[$character->race];					if($character->hair == 1) {						$hair = 'a';					}					else {						$hair = 'b';						}					$str = $race.$character->gender.$character->face.$hair;						echo "<img src=\"$img$str.jpg\" class=\"char_thumb\"/>";					echo "<button type='button' onclick=\"cn_delete('$cn_id');\">Delete</button>";										echo "</div>";					echo "<div class='char_col2'>";						echo "<div class=\"charDetail\">									<ul class=\"tabs\">									<li><a class=\"selected\" onclick=\"doTabClass(this);\">Ranks</a></li>									<li><a onclick=\"doTabClass(this);\">Jobs</a></li>									<li><a onclick=\"doTabClass(this);\">Merits</a></li>									<li><a onclick=\"doTabClass(this);\">Items</a></li>									</ul>																											<div id='cn_merits_$cn_id'>										<ul>											<li>Bastok</li>											<li>												$character->mission_rank->bastok											</li>										</ul>									</div>																																																															<div id='cn_jobs_$cn_id'>Tab 2</div>									<div id='cn_merits_$cn_id'>Tab 3</div>									<div id='cn_items_$cn_id'>Tab 4</div>								</div>											";					echo "</div>";															echo "<div class=\"clear\"/></div>";							echo "</div>";			$cn_id++;			}								}?>

userClass.php

<?php/* A class to store user information. * * * * * * * **/class user {		var $username;	var $fname;	var $lname;	var $password;	var $dob;	var $gender;	var $email;	var $expansion;	var $character;		function user() {		$this->username = false;		$this->fname = false;		$this->lname = false;		$this->password = false;		$this->dob = false;		$this->gender = false;		$this->email = false;		$this->expansion = false;		$this->character = false;	}		}class expansion {	var $zm;	var $cop;	var $tau;	var $wog;		function expansion () {		$this->zm = false;		$this->cop = false;		$this->tau = false;		$this->wog = false;	}}class character {	var $name;	var $race;	var $gender;	var $face;	var $hair;	var $mission_rank;		function character() {		$this->name = false;		$this->race = false;		$this->gender = false;		$this->face = false;		$this->hair = false;		$this->mission_rank = false;	}}class mission_rank{	var $bastok;	var $sandy;	var $windurst;	var $zm;	var $cop;	var $tau;	var $wog;	var $assault;	var $campaign;		function mission_rank() {		$this->bastok = false;		$this->sandy = false;		$this->windurst = false;		$this->zm = false;		$this->cop = false;		$this->tau = false;		$this->wog = false;		$this->assault = false;		$this->campaign = false;	}}?>

add_character.php

<?phpinclude('../class/userClass.php');session_start();$app_user =& $_SESSION['app_user'];include('../db.php');// character details.$name = strtolower($p['name']);$name = ucfirst($name);$race = $p['race'];$gender = $p['gender'];$face = $p['face'];$hair = $p['hair'];if($race > 2) {	$gender = false;}//$app_suer->character = false;if($app_suer->character === false) {	$app_user->character = array();}$app_user->character[] = new character;$i = count($app_user->character)-1;$app_user->character[$i]->name = $name;$app_user->character[$i]->race = $race;$app_user->character[$i]->gender = $gender;$app_user->character[$i]->face = $face;$app_user->character[$i]->hair = $hair;$app_user->character[$i]->mission_rank = new mission_rank;$app_user->character[$i]->mission_rank->bastok = 10;?>

The reason why I want to use objects and asign it to a session variable is because I can store more in an object. If I try and store all the users info in session vars I will end up with too many.so my user object holds character objects and then user object assigned to session var. Is there a better approach to this problem? I'm trying to store a user and their characters details when they are submitting an application to the site. I will then store it in a database once its finsihed and complete.

post-45102-1200993750_thumb.png

post-45102-1200993776_thumb.png

Share this post


Link to post
Share on other sites

You might already know this, but in PHP5, OOP has been rewritten. I'm no expert in this field of PHP, as i've just gotten into learning it, but instead of giving that variable a boolean value of false, try 0 (zero) instead. It should bear the same results if you were to include it into an if statement.

Share this post


Link to post
Share on other sites

Yeah objects in PHP are fairly new. I think it was only brought in in php 4 and it has changed abit in version. I've read on php.net that a class can only have one inheritence. I'm fairly new to OOP style of PHP as well but I think it is much more suited to my current problem. When you say try assigning it zero do you mean my object fields when I create them? I will give it a go I just am not sure if thats what you mean.

Share this post


Link to post
Share on other sites

When you say try assigning it zero do you mean my object fields when I create them? I will give it a go I just am not sure if thats what you mean.

All the variables in userClass.php, instead of assigning them false, give them a zero. I don't know if this will help or not, but i would assume it won't have trouble converting an integer into a string as it did with false.

Share this post


Link to post
Share on other sites

I'm trying that right now I just changed all the false to 0 valuebut I got a whole bunch of new errors not sure what exactly but I'll try narrow it down.

Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 12Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14Warning: Cannot use a scalar value as an array in /Applications/xampp/xamppfiles/htdocs/nexus/includes/forms/add_character.php on line 14

Update:I was able to get it to work. The problem was echoing the object inside double quotes didnt work.the fix was minor but man it took me a while to figure that out.

<li>Bastok</li>											<li>";											echo	$character->mission_rank->bastok;										echo"</li>

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.