Jak Posted December 20, 2006 Share Posted December 20, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/31411-is-this-bad-practice/ Share on other sites More sharing options...
linuxdream Posted December 20, 2006 Share Posted December 20, 2006 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]<?phpclass foo{ var $name = "Brandon"; function foo(){ echo $name; }}class bar extends foo{ function bar(){ echo $name; }}?>[/code]Both classes should echo "Brandon". Quote Link to comment https://forums.phpfreaks.com/topic/31411-is-this-bad-practice/#findComment-145417 Share on other sites More sharing options...
Jak Posted December 20, 2006 Author Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31411-is-this-bad-practice/#findComment-145426 Share on other sites More sharing options...
linuxdream Posted December 20, 2006 Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31411-is-this-bad-practice/#findComment-145441 Share on other sites More sharing options...
Jak Posted December 20, 2006 Author Share Posted December 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/31411-is-this-bad-practice/#findComment-145449 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.