ianmac Posted July 26, 2009 Share Posted July 26, 2009 Hi there I am new to php, I have done a lot of development with classic asp and asp.net. I’ve decided to give php a try so far I have be really enjoy it but I’ve come across a problem. I have 2 classes a template class which fetches template html as a sting and a cms class which reads a string and replaces custom tags with stuff from the database. I have them working independently but now I need that classes to share there variables and methods. Here is a simplified example I have just type this out to give you an outline of what I am try to do. There will be plenty of typos Class template { var temp_html = ‘’; function getHtml($path) { $this->temp_html = file_get_contents($TemplatePath); } Function outputHtml() { return $this->temp_html; } } class cms { var $siteHtml = ‘’; function __construct() { $this->siteHtml = $mytemp->temp_html; } function tagCheck() { // do the tag checking // update template class html updateTemplate() } fuction updateTemplate() { $mytemp->temp_html = $this->siteHtml; } } <?php $mytemp = new template; $mytemp->getHTML(‘index.html’); $mycms = new cms; $nycms-> tagCheck(); echo $mytemp->outputHtml (); ?> I may be going about this the wrong way so any tips or points you could give me would be great. Hope you can help Thanks Mac Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 26, 2009 Share Posted July 26, 2009 Well if the cms class is intended to work on a template object, perhaps you better give it one to work with? A rough example: <?php class foo{ private $var; function __construct($default){ $this->var = $default; } function getVar(){ return $this->var; } } class bar{ private $foo; function __construct($foo){ $this->foo = $foo; } function getFoo(){ return $this->foo; } } $foo = new foo("some value"); $bar = new bar($foo); echo $bar->getFoo()->getVar(); // echo's some value Quote Link to comment Share on other sites More sharing options...
ianmac Posted July 26, 2009 Author Share Posted July 26, 2009 hi GinigerRobot thanks for the reply. I have tried passing the template class into the cms class but it only seem to be an instance of the cms class. is it posible to interact with the $foo class directly? a bit it this class foo{ private $var; function __construct($default){ $this->var = $default; } function getVar(){ return $this->var; } } class bar{ private $foo; function __construct($foo){ $this->foo = $foo; } function changeFoosVar($change2){ $this->foo->var = $change2; } } $foo = new foo("some value"); $bar = new bar($foo); echo $foo->getVar(); // return 'some value' echo $bar->changeFoosVar('some other vaule set by bar'); echo $foo->getVar(); //[color=red]so that is call would the return 'some other vaule set by bar'[/color] thanks again Mac Quote Link to comment Share on other sites More sharing options...
gevans Posted July 26, 2009 Share Posted July 26, 2009 You'll want to pass the object in by reference http://us.php.net/manual/en/language.references.pass.php This will allow you two variables that both point to a single instance of the object. Quote Link to comment Share on other sites More sharing options...
ianmac Posted July 26, 2009 Author Share Posted July 26, 2009 Hi gevans that work a treat thanks every much to both of you your help is very much appreciated Mac Quote Link to comment 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.