Jump to content

Is this bad practice?


Jak

Recommended Posts

Hi,
I'm currently building a simple framework to build my websites on, and there is one thing I am doing a lot that just feels like bad practice.

I am basically creating a lot of parent-child links between my objects, so I create my parent object, and then that uses one of many classes to create a child object (assigned to $this->child), however, because the child object needs access to variables in the parent object, I have to pass the parent object ($this) to the child in the constructor method and then in the child constructor assign that to $this->parent.

Is this bad? I’m doing it quite a lot, and in some cases it goes a couple of levels deep. I can’t think of any other way to do it, as the child objects really are unique to the parents, and they need to know what object they were created by.
Any thoughts?

Thanks in advance,
Jack
Link to comment
Share on other sites

When you say parent/child objects, are the child classes extending the parent classes? From what I know, child classes should extend the parent class and thus inherit all the parent variables and methods unless set to private or something like that.

For instance:

[code]<?php
class foo{
    var $name = "Brandon";

    function foo(){
        echo $name;
    }
}

class bar extends foo{
    function bar(){
        echo $name;
    }
}
?>
[/code]

Both classes should echo "Brandon".
Link to comment
Share on other sites

No, sorry, I don’t mean one class extending another, here is some code:

[code]<?php

class one
{
function __construct()
{
$this->prefs = /*some array of things*/;
$this->db = new databaseConnection(/*user, pass etc...*/);
}

function init()
{
$this->two = new two($this);
}
}

class two
{
function __construct($one)
{
$this->one = $one;
$this->db = $this->one->db;
}

function loadSomething()
{
echo 'My parent is '.get_class($this->one).' i need to use my parents database connection';
$this->db->query(/*SOME SQL*/);
// etc....
}
}

$myOne = new one();

?>[/code]

So the one object has a variable called two, which points to a two object, which contains a variable called one, which points to its parent object.
Link to comment
Share on other sites

Wow, that could get really confusing. Though looking at the example, I don't see why class two couldn't extend class one. It would greatly reduce the confusion and shorten typing time. I think that's what classes are for anyway, to easily manage a group of similar items. If class one and two are referencing the same items, why not combine/extend them? But I can assume there are other methods that might prevent this from happening easily??? Aside form that fact that you probably already have a bunch of code using it this way.

Sorry man, I'm not sure if there is a better practice aside from combining the two classes.
Link to comment
Share on other sites

Yeah, basically one and two are completely different things, so extending them wouldn’t work and doesn’t make sense.
Basically the system I’m building is modular, and based on MVC. Quick explanation:

[list]
[*]There is a “request” class which, based on the URL, loads a module (for example I have a front end “site” module, and a back end “admin” module).
[*]The “one” class in my example above is actually the module class, and it does things like create a variable with the path to the folder that module is in (for including related files later) and creating the database connection.
[*]The module then initializes one of many controller classes (“two” above) which deals with processing the actual request (add a product, view a product etc.).
[*]The controller needs to use things like the database connection that was created by the module class, and so that’s why I need to parse the module object, to the controller.

[/list]

I don’t know if this makes sense, but basically one and two are very different things, and so you wouldn’t use extends.
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.