BaneStar007 Posted May 10, 2008 Share Posted May 10, 2008 I know its bad netiquette to jump into a forum with a question.. but after an hour, and now on this site I've done the search and read the Three there, but they either make no sense, or are not relevant to my Question. If I pass an Object to a function, why does the object cease to be a object? (I learnt JAVA first so my code might be a bit.. wonky?) And so others might have the same question later, lets break it down simply.. class bob{ var $gender = 'male'; var $age; function bob($age) { $this->age = $age; } function getAge() { return $this->age; } function older($newage) { $this->age += $newage; } } // end of Class Nice simple class created Now I have a function in a later program.. function nextYear($character) { $character->older(1); } As you can see, The function nextYear should run the older() function on the object $character. But, call me slow,.. I cant understand what the & and the :: have go to do with it, if it even does.. or have I done something else wrong??? Thanks for your time. Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/ Share on other sites More sharing options...
448191 Posted May 10, 2008 Share Posted May 10, 2008 "Call to Member function on a non-object" means just that. Possibly your dealing with the PHP 'equivalent' of a NullPointerException, e.g. you're passing null instead of the expected object. Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/#findComment-537476 Share on other sites More sharing options...
BaneStar007 Posted May 10, 2008 Author Share Posted May 10, 2008 I have confirmed before and after the function that the variable exists, but it exists not as an object.. .. EDIT.. No, I confirmed that SOMETHING existed, have gone back and checked, thankyou for pushing my mid to a different tangent. Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/#findComment-537479 Share on other sites More sharing options...
448191 Posted May 10, 2008 Share Posted May 10, 2008 I have confirmed before and after the function that the variable exists, but it exists not as an object.. And? (whayasayin?) Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/#findComment-537483 Share on other sites More sharing options...
nloding Posted May 12, 2008 Share Posted May 12, 2008 But, call me slow,.. I cant understand what the & and the :: have go to do with it, if it even does.. or have I done something else wrong??? I don't know if your original problem is fixed or not, but to vaguely answer your questions here, the '&' operator passes something by reference, which is to say the ACTUAL thing itself (be it object or variable). See the PHP manual. If you have $a and you pass it to function addOne($var), $var and $a have the same VALUE, but aren't the same in memory -- in other words, if you change $var, $a stays the same. If you pass by reference (addOne(&$var)), then they are the same and what you do to $var happens to $a. <?php function addOne($var) { $var++; } function addOneRef(&$var) { $var++; } $a = 1; addOne($a); // $a = 1, $var = 2 addOneRef($a); // $a = 2 ?> That's probably way over simplified, read the manual. As for the '::' operator, that's for static functions within the class. A static function can be run without the class being instantiated. So instead of doing: $object = new Object; $object->method(), you can just do Object::method() ... however, that method/object must be a static object. Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/#findComment-538758 Share on other sites More sharing options...
aschk Posted May 13, 2008 Share Posted May 13, 2008 It's worth noting that in PHP4 objects assigned to variables were NOT references, but as of PHP5 they are. i.e. in 4 $test = new Class(); is NOT the same as: $test = new Class(); in 5 So have a reference to an object in 4 you needed to do $test &= new Class(); Now in 5, references should only really be used for non-object variables, as all objects are passed around by reference. Link to comment https://forums.phpfreaks.com/topic/105005-fatal-error-call-to-member-function-on-a-non-object/#findComment-539750 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.