I’m coming from Objective-C, where object oriented programming is just a dream. (at least for me)
I have an issue with this in PHP. I’m trying to make a Model-Class to save my database entries. It’s looking like this:
class Model {
public function __set($name, $value)
{
$methodName = "set" . ucfirst($name);
if (method_exists($this, $methodName)) {
$methodName($value);
} else {
print("Setter method does not exists");
}
}
};
I’d like to subclass this and make a class User.
class User extends Model {
private $userID;
public function userID() {
return $this->userID;
}
public function setUserID($theUserID) {
$this->userID = $theUserID;
}
};
When I call $user->__set("userID", "12345"); I get the following exception:
Fatal error: Call to undefined function setUserID() in Model.class.php
The $user object of course is a User object. Why can’t I call methods from a superclass?
you are checking existence of a method in object
(method_exists($this, $methodName))and than calling function, not this object method, should be:$this->$methodName($value);