TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Using class methods in an included file? (http://www.talkphp.com/advanced-php-programming/1778-using-class-methods-included-file.html)

Andrew 12-20-2007 04:21 AM

Using class methods in an included file?
 
Hello,

I'm creating a script, and I'm coding the templating system at the moment. The problem I'm running into, is that I am using class methods and variables (using the __call() magic method), in a template file, which includes no classes nor defines them.. I thought that as long as the parent file initializes the classes, I could use them in an included file in the same parent file. Here is my code:

Index.php:
PHP Code:

include_once 'includes/global.php';

try {
    
$tpl->header();
    
/*if (isset($_GET['page'])) {
        $tpl->index();
    } elseif(isset($_GET['wallpaper'])) {
        $tpl->single();
    }*/
} catch (Exception $pEx) {
    echo 
$pEx->getMessage();


Global.php:
PHP Code:

require_once './config.php';
function 
__autoload($szClassName) {
    require_once 
strtolower($szClassName).'.class.php';
}

$user = new User;
$site = new Settings;
$tpl = new Template($site->theme_directory()); 

And the method I'm trying to use is $site->site_name(); (which I know for a fact is a valid method using __call()). The problem is, I'm getting this error:
Code:

Fatal error: Call to a member function site_name() on a non-object in C:\xampp\htdocs\PHP\WallpaperScript\templates\default\header.php on line 6
Which shows that my class isn't defined, which goes against my thought that it would work since the global.php was included in the parent file which is including the template file.

How do I get around this?

maZtah 12-20-2007 10:57 AM

What I'm doing (using the Kohana framework) is to render a View but also passing an array to the View with all the needed data. Like this:

PHP Code:

$aData = array(
    
'szTitle' => 'Now this is a nice title!',
    
'szHeader' => 'And this is the header!'
)
$pView $this->load->view('indexView'$aData);
$pView->render(TRUE); 

In the indexView.php you then can simple call/echo $szTitle and $szHeader.
I hope this helps you a bit.

Wildhoney 12-20-2007 12:22 PM

Not bad. Can you pass them through individually as well? I remember in CakePHP it was like:

php Code:
$this->set('myVar', 'myData');

A bit slow, but I'm sure if I went deeper into it they'd be an array way as well.

Salathe 12-20-2007 12:39 PM

This is going off topic, but Kohana takes the View as just another object to be manipulated. Member variables can be set dynamically which are then available in the templates files. For example:

PHP Code:
// Code in Controller
$view = new View('mytemplate');
$view->myVar = 'myData';
$view->render(TRUE);

// In the template
<strong><?php echo $myVar; ?></strong>

Back to the original topic, chances are that you're looking at an issue of scope. The header appears to be loaded inside of a class method (on the $tpl object) so the global variables ($user, $site ...) will not be available unless you're using 'global $site' to provide access, or $GLOBALS['site'], or passing those into the template class/methods.

For a test, you could try and var_dump($site) (from inside the header() method) just to see if the object is available to use.

Andrew 12-20-2007 07:44 PM

Quote:

Originally Posted by Salathe (Post 6926)
This is going off topic, but Kohana takes the View as just another object to be manipulated. Member variables can be set dynamically which are then available in the templates files. For example:

PHP Code:
// Code in Controller
$view = new View('mytemplate');
$view->myVar = 'myData';
$view->render(TRUE);

// In the template
<strong><?php echo $myVar; ?></strong>

Back to the original topic, chances are that you're looking at an issue of scope. The header appears to be loaded inside of a class method (on the $tpl object) so the global variables ($user, $site ...) will not be available unless you're using 'global $site' to provide access, or $GLOBALS['site'], or passing those into the template class/methods.

For a test, you could try and var_dump($site) (from inside the header() method) just to see if the object is available to use.

It's displaying "NULL". In my class declarations for $user, $site, how do I declare those as global? Like this?
PHP Code:

global $var;
$var = new Class; 

Edit: Tried that, and still isn't working.

Salathe 12-20-2007 11:20 PM

The use of 'global' is covered well in the PHP manual: Variable scope.

Andrew 12-23-2007 03:20 AM

I was just trying out a few placements and got it working eventually. Thanks a lot Salathe! :)


All times are GMT. The time now is 12:56 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0