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

Link to comment
Share on other sites

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

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.