PHP Multiple Classes

Mteigers

New Member
Basically I'm wondering how to instantiate another class while already within a class.. I know it's possible I'm just not sure...

Here's basically what I need:
Template File - Holds the page that you're going to see (The RAW HTML), this file instantiates the $INIT class.

init.php - This is the class that holds all the pre-defined variables as well I'm HOPING this is where we'll instantiate additional classes like $Skin to make the template Dynamic.


So hopefully once I've instantiated the two successfully I can call normal predefined global variables like $INIT->GLOBALURL; as well as call functions from other classes like so: $INIT->Skin->Do_HTML_Header();

Any ideas?
 

saviola

New Member
I can't understand everything, but on first look it seems to me the INIT class must extend the 'Skin' classs if you want to pre-define some variables of class Skin.
If you want to call a different function between two classes you can use the Deledator pattern. Who is :
PHP:
<?php
class Delgator {
    private $targets    = array();
	function __construct($object) {
	   $this->targets[]    = $object;
	}
	public function __call($methodName, $args) {
	   foreach ( $this->targets as $obj) {
	       $reflector  = new ReflectionClass($obj);
	       try {
	           if ( $method    = $reflector->getMethod($methodName)) {
	               if ( $method->isPublic() && !$method->isAbstract() ) {
	                   return $method->invoke($obj, $args);
	               }
	           }
	       } catch ( ReflectionException $e ) {
	           echo $e->getMessage();
	       }
	   }
	}
}
?>

<?php

class SomeClass {
	function display ($counrter) {
		for($i; $i < $counrter[0]; $i++ ) {
			print "Abracadabra \n";
		}
		return $counrter;
	}
}
?>

index.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'].'Delegator.php');
require_once($_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'].'SomeClass.php');

$obj    = new SomeClass();
$obj2   = new Delgator($obj);

$obj2->display(5);
?>

This maybe can help you. If it isn's you must see more depth OOP programme and design patterns.
 

Mteigers

New Member
Thanks for that.. I ended up doing a more lazy approach which seems to be working just fine basically I created my own function to dynamically call and create my class, calling the correct function

Here's what I did:
Code:
function INIT($name, $value = "", $param = 1)
{
	$INIT = new Init();
	if($value != "") {
		if($param == 1) {
			$INIT -> $name($INIT -> $value);
		} else {
			$INIT -> $name($value);
		}
	} else {
		return $INIT-> $name;
	}
}

So now instead of doing $INIT->ImgLocation($INIT->GLOBALURL);
I can call it much easier by doing INIT("ImgLocation", "GLOBALURL");
 
Top