50r Posted November 3, 2012 Share Posted November 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/ Share on other sites More sharing options...
gizmola Posted November 3, 2012 Share Posted November 3, 2012 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". Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389899 Share on other sites More sharing options...
50r Posted November 3, 2012 Author Share Posted November 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389902 Share on other sites More sharing options...
gizmola Posted November 3, 2012 Share Posted November 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389904 Share on other sites More sharing options...
50r Posted November 3, 2012 Author Share Posted November 3, 2012 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 />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389905 Share on other sites More sharing options...
ignace Posted November 3, 2012 Share Posted November 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389926 Share on other sites More sharing options...
50r Posted November 3, 2012 Author Share Posted November 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389959 Share on other sites More sharing options...
hell_yeah Posted November 3, 2012 Share Posted November 3, 2012 (edited) 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 November 3, 2012 by hell_yeah Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1389971 Share on other sites More sharing options...
ignace Posted November 3, 2012 Share Posted November 3, 2012 (edited) 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 November 3, 2012 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1390006 Share on other sites More sharing options...
50r Posted November 3, 2012 Author Share Posted November 3, 2012 So this means which ever way atributes have to be restricted to eith public ,private or protected? Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1390007 Share on other sites More sharing options...
ignace Posted November 3, 2012 Share Posted November 3, 2012 Yes. var is the same as public, but I strongly recommend you adhere to the set* get* methods instead of allowing direct modification of the properties. Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1390012 Share on other sites More sharing options...
50r Posted November 4, 2012 Author Share Posted November 4, 2012 thank men i will try and make a good understanding of the getters and setters. Quote Link to comment https://forums.phpfreaks.com/topic/270240-referencing-problem-in-oop/#findComment-1390135 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.