tags:

views:

161

answers:

5

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

+1  A: 

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

jspcal
Hi I just realized it is working for including the page, It just breaks any class/method calls I make inside the header file for some reason
jasondavis
the session class is currently started in the config.inc.php file, is it possible to use it from there since config file is loaded before the header file is included?
jasondavis
yeah, in that case you'd need to `global $session` in the include
jspcal
The problem is you're erroneously expecting code you execute from within a function show_header to have knowledge of variables that exist outside of them.
hobodave
This is a dissappointment, because in my header file I have many objects being called besides the session one so I would need to make like 10 objects Global in the header file, looks like I might be better off just including the header file into all my files unless I can find a better way to do this, thanks for the help
jasondavis
why not define a context object and reference all the other objects through that... `$context = WebContext::get(); $context->session; $context->input; $context->identity`, etc...
jspcal
Honestly I don't really understand it enough, maybe I will research this more first, thanks!
jasondavis
I actually found a better solution I think, I remove all the class/object calls fromt he header file and add them into my header method and then include the header file and that makes it all work better it seems
jasondavis
yeah you can do that as well... but what if you need those objects from another nested function? you might check out ZF (zendframework) or CI (codeigniter)..
jspcal
+1  A: 

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

hobodave
That seems to be the exact problem I am having, is there anyway around this besides declaring a object as Global?
jasondavis
Yes, by not doing what you're doing. I'd suggest using a framework, or looking at how a framework is built before you begin trying to build your own. When I include files, they almost _never_ execute code, but just contain class/function definitions.
hobodave
I edited my answer to show an alternate approach for you
hobodave
A: 

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

Emil Vikström
A: 

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

Scott
A: 

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.

spatel