Tempesta Posted July 7, 2012 Share Posted July 7, 2012 Hey guys, I'm just getting into php OOP and have a little issue. Basically I'm building a CMS and page types are extending a base object like this : BaseObject -> Page -> Blog. What I'm trying to do here is this. Each of these classes has their own class instance of "DatabaseInteracter" that handles data. This "DatabaseInteracter" class is referenced in " protected $DatabaseInteracter;", this is all fine and dandy. The only problem is that I have to manually create the reference variable in each class, since it's protected an unique for each class. This is kind of annoying, and it would be great if I could do this automatically somehow. Is there a way to make each instance that extends BaseObject have the "protected $DatabaseInteracter;" already inlcuded? Sort of like a helper/abstract class that contains this variable, that BaseObject can extend or implement? This is kind of confusing even to me, so bare with me. If you understand the question and think you know what I'm trying to do, let me know! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/ Share on other sites More sharing options...
trq Posted July 7, 2012 Share Posted July 7, 2012 If you declare $DatabaseInteracter as protected within your BaseObject it will be available within all child classes. Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359832 Share on other sites More sharing options...
Tempesta Posted July 7, 2012 Author Share Posted July 7, 2012 Thanks for your reply but I meant "Private" not "Protected", my bad. Anyways I worked around it and put everything in a public array. So now I call a function called "createTable($tableName)" on each class that put a new instance of "DatabaseInteracter" in the array, now what would be great is if I can make it associative and put the class name as the key. So that in the constructor of each of the classes (BaseObject, Page and Blog) I run the createTable function. running "$page = new Page();" will do the constructor of each class. But get_class($this) = "Blog" in all of the constructors. Is there a way I can get the scope of each of the constructors so that get_class($this) returns "BaseObject", "Page" and "Blog" respectively? My goal is to get this array( "BaseObject" => new DatabaseInteracter, "Page" => new DatabaseInteracter, "Blog" => new DatabaseInteracter ) after I construct a new Blog. Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359921 Share on other sites More sharing options...
xyph Posted July 7, 2012 Share Posted July 7, 2012 It's a little tricky to follow your logic. I'd suggest using a variant or clone of an existing design pattern. Why would each one of those need it's own DatabaseInteracter? (it's interactor btw) If blog extends page extends base, then only base needs to declare a DatabaseInteractor, all of the child classes will inherit it's existence. Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359923 Share on other sites More sharing options...
Tempesta Posted July 7, 2012 Author Share Posted July 7, 2012 I guess I could create one big DatabaseInteractor(thanks) that handles all the interactions instead, I just felt it would be nice and tidy for them to have one each, seeing as how each step has one database table each. Thanks for your input Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359933 Share on other sites More sharing options...
xyph Posted July 7, 2012 Share Posted July 7, 2012 What? Why would each 'step' have it's own table? You extend classes to share structure and functionality, not data. Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359943 Share on other sites More sharing options...
Tempesta Posted July 7, 2012 Author Share Posted July 7, 2012 Cause a new class requires new data. Like when the blog extends page, I would like the blog page to have access to all the data that the page has, e.g its ID. But it also needs additional data, so I create a new table in my DB. And now it has both sets of data and all the functionality of the Page and BaseObject class. Doesn't that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359945 Share on other sites More sharing options...
xyph Posted July 7, 2012 Share Posted July 7, 2012 It's a very unorthodox way of doing it. I'd suggest simply passing the page object into the blog object. That way, the blog object will have access to any data the page object might contain. Quote Link to comment https://forums.phpfreaks.com/topic/265341-automatically-creating-protected-variable-on-extend/#findComment-1359949 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.