Parhs Posted December 3, 2008 Share Posted December 3, 2008 Hello to all First of all sorry for my English. Although i have programming experience i havent used object oriented design a lot. I know what are classes inheritance etc but i amnt sure wether i do things correctly.. Where should i start? Some books i found didnt help a lot..I want to see real examples,how others program in OOP. Also i am thinking to make a project in PHP using OOP and i am unsure how to do things. For example one simple question i have.. Suppose that i have a Database class which has some methods to retrieve and send data to database.And suppose that i have a News and Users class. Suppose i have some functions News->getNews(blabla) Users->getUserInfo(blabla) ... The question is.. Should i create a database object $database=new Database(); and pass this instance of the object at the News or Users class? e.g getNews(blabla,$database) Or getNews() should create a Database instance object ? I cant find any answer on this please help! Quote Link to comment https://forums.phpfreaks.com/topic/135353-beginner-oop-some-questions/ Share on other sites More sharing options...
JonnoTheDev Posted December 3, 2008 Share Posted December 3, 2008 Its entirely up to you. There are no fixed rules for OOP. The concept of OOP is about reusability, so could you use the user class in another project by itself or is it coupled tightly to your db object (i.e. would you have to take both). If you want to use the db object in you user class then why not extend it i.e (using a made up database object). class user { public $name; } class dbUser extends user { private $db; // pass a new db object in public function __construct(db $db) { $this->db = $db; } public function getUser($id) { $this->db->query("SELECT * FROM user WHERE id='".$id."'"); $result = $this->db->record(); $this->name = $result['name']; } } // usage $user = new dbUser(new db()); $user->getUser(12); print $user->name; Quote Link to comment https://forums.phpfreaks.com/topic/135353-beginner-oop-some-questions/#findComment-705109 Share on other sites More sharing options...
Parhs Posted December 3, 2008 Author Share Posted December 3, 2008 I see.Nice solution though.Thank you for sharing. Quote Link to comment https://forums.phpfreaks.com/topic/135353-beginner-oop-some-questions/#findComment-705213 Share on other sites More sharing options...
DarkWater Posted December 3, 2008 Share Posted December 3, 2008 A User object has nothing to do with a database; queries should not be delegated to the User object. You'd probably want a data mapper that creates new User objects. Anyway, I'd suggest reading up on design patterns. Quote Link to comment https://forums.phpfreaks.com/topic/135353-beginner-oop-some-questions/#findComment-705486 Share on other sites More sharing options...
Eric_Ryk Posted December 4, 2008 Share Posted December 4, 2008 Another good thing to look at is PHP frameworks. Not necessarily to use, but to break them down and examine their structure. You can learn quite a bit about application design from doing this. As DarkWater said, read up on design patterns. Quote Link to comment https://forums.phpfreaks.com/topic/135353-beginner-oop-some-questions/#findComment-705794 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.