Jump to content

Best way to use a Class inside a Class?


greatbigmassive

Recommended Posts

Hi there,

I'm kind of new to classes but want to master it.

I've looked up as much as I can on PHP.net and I need some plain english help at this stage.

I'm trying to understand the best process for doing the following.

Maybe you guys can help, it's been driving me a little mad trying to work out the best way.

 

My problem

========

I have 2 Classes.

class core{

class html{

 

I call $core at the very beginning of all scripts.

$core = new core;

 

What they do

===========

-> The core class controls all of the core actions required to run the website and holds some important variables and functions regarding the environment.

 

-> The html class is only used when I need to output html so I don't need it until html is getting printed.

 

Sometimes I want to use elements from the core class inside the html class and vice-versa.

What is the best way of doing this?

 

A Situation

----------------

Everytime I want to create a system HTML link I use:

 

$html->url('My Action');

 

Inside the $html->url  function I have to use a procedure from the $core class in order to use use the environments URL to prepend it to the action. The problem is that $core isn't global so I have to get it every time I use the $html->url function

 

This means I have to do

global $core;

in each $html->function that uses it.

 

I've also looked at the "mediator class" that I can use to keep track of objects and re-use them but I still have to call it everytime I'm in the $html->functions

 

e.g

$core =& mediator:fetch("core");

 

 

Knowing that I could use the $html->url function say... 15 times on a page, this means it does a global on $core each time or uses the mediator call each time. Is this ok???

 

So the question really is, what's the best way of optimising this knowing that in most of the $html class functions I need to use the $core class?

 

Class expert needed here :)

Hope you can help, thanks!

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

So the question really is, what's the best way of optimising this knowing that in most of the $html class functions I need to use the $core class?

 

If most of the $html classes functionality requires $core, it may actually be worthwhile making $html extend $core.

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.