TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-20-2007, 04:21 AM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default 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?
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 12-20-2007, 10:57 AM   #2 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

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.
maZtah is offline  
Reply With Quote
Old 12-20-2007, 12:22 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-20-2007, 12:39 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 12-20-2007, 07:44 PM   #5 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 12-20-2007, 11:20 PM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The use of 'global' is covered well in the PHP manual: Variable scope.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Andrew (12-23-2007)
Old 12-23-2007, 03:20 AM   #7 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

I was just trying out a few placements and got it working eventually. Thanks a lot Salathe! :)
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:09 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design