开发者

Can I have a class method include a header file for me in PHP?

Please review the example code below, I have a class file that is loaded into a config file. The config file is then loaded into any page I build. Is it possible to include a header file the way I have in the show_header() method? It doesn't seem to work so how can I achieve this result?

// Core.class.php
class Core
{

    public function show_header($page_name){
        require_once 'includes/header.inc.php';
    }

}

// config.inc.php
require_once 'Core.class.php';
$core = New core;



// testpage.php
require_once 'config.inc.php';
$core->show_header('home');

Here is the top pa开发者_开发问答rt of the header.inc.php file I am trying to include into the page, it seems to work including it but it breaks the way the header file works.

//header.inc.php
<?PHP

//start page timer
$session->get('user_id');
$profiler = new Profiler;
$profiler->start();


//see if site is turned on/off
$core->sitestatus($config['site_status']); 

This part gives me errors like this...

Notice: Undefined variable: session in C:\webserver\htdocs\friendproject2\includes\header.inc.php on line 5

Fatal error: Call to a member function get() on a non-object in C:\webserver\htdocs\friendproject2\includes\header.inc.php on line 5


When you're including a file from within a function it's just as if you wrote the code within that file from within that function.

e.g.

file foo.php:

<?php
echo $foo->getFoo();

file bar.php

<?php
class Foo {
    public function getFoo() {return 'foo';}
}

$foo = new Foo();

function bar()
{
    require 'foo.php';
}
bar();

The above will result in the following notice/error being thrown, because $foo is not known within bar().

Fatal error: Call to a member function getFoo() on a non-object in /Users/hobodave/foo.php on line 3

Edit:

I'm not sure what your "Core" class fully entails, but you could perhaps use it as a type of storage for your "globals".

e.g.

<?php
$session = new Session();
$core->session = $session;

Then your $session would be accessible in your header using $this->session


re your comment, sounds like you need a root web context object that you reference the other objects from:

$ctx = WebContext::get();
$ctx->session->get('x');
$ctx->input->get('y');
$ctx->identity->valid;

etc... this is how most web frameworks do it.

$session would need to be defined, then referenced in the included file:

// If a global variable:
global $session;
$session->get('x');

// If a member of Core:
$this->session->get('x');

yes you can do that, probably you'll want require instead of require_once, and the paths would need to be based on the current working directory or an absolute path

try adding error_reporting(E_ALL) to see if any notices are happening...


All calls you make inside the header file will be called as if they were local calls inside the show_header function. So if you want to use any global variable, you will have to use global $variablename; on the top of the included file (or in the beginning of the show_header function).


If you use a static function for the session class you wouldn't need to define it in the same file. http://php.net/manual/en/language.oop5.static.php


You are trying to access $session which is out of scope as pointed in another answer.

Since session stuff is usually global throughout most apps consider using the singleton pattern for the Session class.

This way you can do something like $session = Session::getInstance().

This lets you use the session class anywhere and you usually only one need one instance of a session class (usually). Take a look at Zend Framework for examples on singleton classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜