php newbie class
class Theme
{
function __construct()
{
}
function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
require_once($theme_path);
return TRUE;
}
}
on index.php
<?php
require class.theme.php
$theme = new Theme;
$theme->load('site','index');
?>
on my site/index.php
<?php
// to work i need another $theme = new theme; why can i do this ? can i ma开发者_StackOverflow社区ke
it make it work twice or more inside the function load ?
$theme->load('site','header');
$theme->load('site','footer');
?>
somehow it needs to $theme = new Theme; again on site/index.php
is there another way to make it work? maybe my class design is not good or algorithm is failing.
edit* more information ok so what im trying to do is to load header view footer view.
We don't know the relationship between your two .php files so it would be difficult to answer.
If you define $theme as new theme, scoping rules still apply: you definition/instanciation is only valid on its scope. You won't have a global theme object. Independtly from any class/object design.
The object "$theme" doesn't persist throughout several files, so when "site/index.php" is requested, your object from "index.php" is gone ...
Either that or I got your question completely wrong :)
Try to make load function public:
class Theme
{
function __construct()
{
}
public static function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
require_once($theme_path);
return TRUE;
}
}
class Theme
{
function __construct()
{
}
function load( $folder, $file )
{
$theme_path = ROOTPATH . '/theme/' . $folder . '/' . $file . '.php';
return $theme_path;
}
}
on index.php
<?php
require class.theme.php
$theme = new Theme;
require_once $theme->load('site','index');
?>
on my site/index.php
<?php
// to work i need another $theme = new theme; why can i do this ? can i make
it make it work twice or more inside the function load ?
require_once $theme->load('site','header');
require_once $theme->load('site','footer');
?>
this done the trick for the while, thanks guys.
精彩评论