Jump to content

sharing variables and methods


ianmac

Recommended Posts

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  :D

 

 

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

 

Link to comment
https://forums.phpfreaks.com/topic/167480-sharing-variables-and-methods/
Share on other sites

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

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  :D

 

Mac

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.