Using class in a class. (Without 'extends')<

liunx

Guest
///header.php
<?php
include("class.FastTemplate.php");
$tpl = new FastTemplate("tpl/");
include("class.Site.php");
$prc = new GQ;
?>



///class.Site.php
<?php
class GQ {
global $tpl;
function login($username,$password){
$tpl->assign(MESSAGE,"loggedin");
}

}
?>


I can't use the FastTemplate class, even when i global $tpl, how can i get it to work?

I don't want to extend the FastTemplate class to keep them sperated.Is aggregation satisfactory?


<?php
// header.php

include("class.FastTemplate.php");
include("class.Site.php");

$tpl = &new FastTemplate("tpl/");
$prc = &new GQ($tpl);

?>



<?php
// class.Site.php

class GQ {

var $tpl;

function GQ(&$tpl) {
$this->tpl = &$tpl;
}

function login($username, $password) {
$this->tpl->assign(MESSAGE, "loggedin");
}

}
?>


Code whipped up in 3 sec and not tested.works ok..though all the extra typings does.. nevermind thanks ;)Well that's how it's most commonly done, and how I would do it. Note also the carefully placed &'s, as they will make your scripts more efficient(until PHP5 arrives and changes how objects are passed).damn...sorry to bother you again, hope this isn't too stupid :P

how would i pass an array form header.php into the class ?

like:

<?php
include("class.FastTemplate.php");
include("class.GalacticQuest.php");
$tpl = &new FastTemplate("tpl/");
$lang = array();
$lang['empty_field'] = "Please fill in all fields.<br>";
$prc = &new GQ($tpl,$lang);


and the class file...

<?php
class GQ {

var $tpl;
var $lang;

function GQ(&$tpl) {
$this->tpl = &$tpl;
$this->lang = $lang;
}

god i feel dumb muahahah<?php
class GQ {

var $tpl;
var $lang;

function GQ(&$tpl, &$lang) {
$this->tpl = &$tpl;
$this->lang = &$lang;
}


Like this? Simply add the second parameter to the GQ constructor. Arrays are also to be passed as references, unless, of course, you want a copy of the array inside the class.
 
Back
Top