Jump to content

Referencing Problem In Oop


50r

Recommended Posts

Hello gurus i am new to oop and am trying to teach my self but am stack on referencing i created a class Car and created some properties in it.

now as am studting to reference one class into another, i created another class NewCar extends Car

but now the problem is when i instatiate NewCar and save it to a valuable and try to echo that valuable i get this error.

Catchable fatal error: Object of class Newcar could not be converted to string in C:\xampp\htdocs\sandbox\inheritance.php on line 20

 

what could be the problem from the tutorials it seems right or which is the better way to reference aclass

Link to comment
Share on other sites

You need to post the code for inheritance.php. From the looks of the error, you are probably trying something like:

 

$newcar = new Newcar();
// Sometime later
echo $newcar;

 

echo expects a string, not an object. As an alternative you can try var_dump($newcar), if you just want quick and dirty debug output while you are learning.

 

It is also possible to implement the php magic method __toString in your class, which will allow you to echo or print an object of your class, but that requires you to write the code that provides something "printable".

Link to comment
Share on other sites

in otherwords i cant echo an object directly i must echo either methods or propertis in an object. but now how do i use _toString just a quick clue as i make my way to php.net.

 

and by the ways thanks for the quick replay.

Link to comment
Share on other sites

Correct. echo requires a string, but as php is loosely typed, you can echo any of the basic types. You can not echo compound types like arrays or objects.

 

If however, your class has a method that returns a base type you can echo that:

 

echo $myobj->somefunction();

 

magic methods like __toString have special behavior, but in terms of writing them, they are just like any other method, only they are required to have a special name.

Link to comment
Share on other sites

thanks it worked now just learning oop though hahaha

 

<?php
class Car{
   var $doors = 2;
   var $wheels = 4;
   function doorwheel(){
   return $this-> doors + $this->wheels;        
   }
}
class Compactcar extends Car{

}

$car = new Car;
$Ccar = new Compactcar;

echo $car->wheels. "<br />";
echo $car->doorwheel(). "<br />"."<br />";
echo $Ccar->wheels. "<br />";
echo $Ccar->doorwheel(). "<br />"."<br />";

echo "Parent Class of Car is:". get_parent_class('Car')."<br />";
echo "Parent Class of Compact Car is:". get_parent_class('Compactcar')."<br />"."<br />";

echo is_subclass_of('Car', 'Compactcar')? 'true' : 'false' ."<br />";
echo is_subclass_of('Compactcar', 'Car')? 'true' : 'false' ."<br />";
?>

Link to comment
Share on other sites

Please use the new php5 syntax (public, private, protected) instead of the old deprecated php4: var.

 

Also it's better to not allow direct modification of your variables otherwise I could do something like:

 

$car->doors = 'foo';
$car->wheels = 'bar';
echo $car->doorwheels(); // 0

$car->doors = array(4);
$car->wheels = array(4);
echo $car->doowheels(); // Array

 

Not really expected behavior. The convention is to use set* and get* methods.

 

class Car {
  private $doors = 2;
  private $wheels = 4;
  
  public function setDoors($doors) {
    if (is_int($doors) && $doors > 1 && $doors <= 5) {
      $this->doors = $doors;
    }
  }
  
  public function setWheels($wheels) {
    if ($wheels == 4)
      $this->wheels = $wheels;
    }
  }
  
  public function getDoors() { return $this->doors; }
  public function getWheels() { return $this->wheels; }
  
  public function doorWheel() {
    return $this->getDoors() + $this->getWheels();
  }
}

$car = new Car;
$car->setDoors(999);
$car->setWheels(999);

echo $car->getDoors(); // 2
echo $car->getWheels(); // 4

Link to comment
Share on other sites

Ok anyway this was my first class but i would like to thank you for the contribution. But now let me ask you you have said that var is no more in use and from the i was studiying this var was used to declare a property or valuable. acording to my understanding, i thought that if i dont include var at the beggining of declaring a property, the declared property wont work.

 

2. the public, private,protected acording to what i studed today were for restrictions scope in which the code can be accessed.

 

so my question is if var was depricated how do i create or declare a property that i have no intention of resctricting? thanks.

Link to comment
Share on other sites

You can simply create a class property by just giving it a valid variable name, example:

class MyClass{

// Variable Declaration Example
$classVar1;
Public $classVar2;
private $classVar3;
}

 

If you don't specify the visibility keywords (i.e. public, protected, or private), then the variable will be considered as public by default. However, for consistency reason, it is always advisable to specify the visibility keyword before the variable declaration. If you have no intention to restrict access to that variable, just use the public

Edited by hell_yeah
Link to comment
Share on other sites

If you don't specify the visibility keywords (i.e.  public, protected, or private), then the variable will be considered as public by default.

 

Please verify what you write before writing it down and assuming:

 

class MyClass {
  $classVar1;
}

 

Results in:

 

Parse error: syntax error, unexpected '$classVar1' (T_VARIABLE), expecting function (T_FUNCTION)

 

Properties should always have a visibility defined (var, public, private, or protected, though the use of var is discouraged).

Edited by ignace
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.