Jump to content

Automatically creating protected variable on extend


Tempesta

Recommended Posts

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 :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.