Jump to content

Why do i get this error (below) ? Class problem...


Recommended Posts

i'm sorry i posted this here, there is no one in oop help forums and i need help urgently...

 

i get this error : Catchable fatal error: Object of class Aeroplane could not be converted to string in C:\AppServ\www\AirFighter.php on line 8

 

this is the php code:

 

<?php

include("aeroplane.php");
$AirFigther=new Aeroplane("2031", "F-16", "US Air Force Fighter");
echo $AirFighter;
echo "<br /><br />";
$UnitedAirlines=new Aeroplane("1220", "Boeing 777", "United Airlines Passenger Aircraft");
echo $UnitedAirlines;
?>

 

the class aeroplane:

 

<?php

class Aeroplane
{
function Aeroplane($no, $type, $usage)
{
     $identity="<b>No:</b> ".$no."<br /><b>Type:</b> ".$type."<br /><b>Usage:</b> ".$usage;
 echo $identity;
}
}

?>

 

 

echo $AirFighter; $AirFighter is an object not a string - you can convert in php5 using the __tostring method http://uk.php.net/manual/en/language.oop5.magic.php

 

otherwise you should be echoing out properties of the object like so...

 

echo $Airfighter->indentity;

 

you will get the same error on line 11 once you have this one sorted....

hello..

 

i ran ur piece of code...i did get the output as

No: 2031

Type: F-16

Usage: US Air Force Fighter

 

No: 1220

Type: Boeing 777

Usage: United Airlines Passenger Aircraft

Catchable fatal error: Object of class Aeroplane could not be converted to string in C:\xampp\htdocs\xampp\temp.php on line 13

 

The problem was in echoing the objects of class Aeroplane.You dont need to do tat since the values are being echoed in the constructor.

 

I am not sure whether i have got u right rgarding 'overriding variables'.

s tis wat u want??

 

include("aeroplane.php");
$AirFigther=new Aeroplane("2031", "F-16", "US Air Force Fighter");

echo "<br /><br />";
$UnitedAirlines=new Aeroplane("1220", "Boeing 777", "United Airlines Passenger Aircraft");
echo "<br /><br />";
$aa=new Aeroplane("1221","aa");
echo "<br /><br />";
$bb=new Aeroplane("1221");

aeroplane.php :

class Aeroplane
{
function Aeroplane($no="0", $type="", $usage="")
{
     $identity="<b>No:</b> ".$no."<br /><b>Type:</b> ".$type."<br /><b>Usage:</b> ".$usage;
 echo $identity;
}
}

 

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.