lanmonkey Posted October 15, 2008 Share Posted October 15, 2008 I understand how it is possible to work with a class in its non-object form using the scope resolution operator ect…. But my question is: Why would you want to? thanks Quote Link to comment https://forums.phpfreaks.com/topic/128516-why-use-an-uninstantiated-class/ Share on other sites More sharing options...
KevinM1 Posted October 15, 2008 Share Posted October 15, 2008 I understand how it is possible to work with a class in its non-object form using the scope resolution operator ect…. But my question is: Why would you want to? thanks Sometimes you don't want an object, but rather one of its methods. Factory classes are a common example of this: class PersonFactory { public static function getPerson($id) { $query = "SELECT * FROM people WHERE id = '$id'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $person = new Person($row['name'], $row['age']); return $person; } } class Person { private $name; private $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $myPerson = PersonFactory::getPerson(22); In this case, you don't need (or even want) to have a hold of a PersonFactory object. You just need a specific Person object. Quote Link to comment https://forums.phpfreaks.com/topic/128516-why-use-an-uninstantiated-class/#findComment-666052 Share on other sites More sharing options...
lanmonkey Posted October 15, 2008 Author Share Posted October 15, 2008 I had considerd this and thought it might be more straightforward to just create a function Quote Link to comment https://forums.phpfreaks.com/topic/128516-why-use-an-uninstantiated-class/#findComment-666088 Share on other sites More sharing options...
Stooney Posted October 15, 2008 Share Posted October 15, 2008 I've found doing so with a registry object can be handy. It makes my registry available from anywhere a global would be, and I never have more than 1 registry object, never have to pass it to other classes, etc. Example. <?php $errors=new Error(); Registry::set('error', $errors); //To use the error object Registry::$vars['error']->add_error('registration', 'You must type a username'); Quote Link to comment https://forums.phpfreaks.com/topic/128516-why-use-an-uninstantiated-class/#findComment-666263 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.