Jump to content

Archived

This topic is now archived and is closed to further replies.

scottybwoy

Classes logic more help needed please ;)

Recommended Posts

I'm using OOP concepts in php5.

I have a main parent class, but above it i have all my constants and configs included in a seperate file like so :

[code]
<?php

require_once 'config.inc.php';

class PHPApplication {
    ....
}
?>
[/code]

Later on in the file a call to another file occurs to display the page.  Within the called file is another class that extends the original but all my configs are gone.  Is there a better way to have my configs included once and be available to all my files?
Thanks

Share this post


Link to post
Share on other sites
Sounds more like a scope issue than to do with the actual includes.

Could you show an example of a few of your config settings? Are they variables, constants or such?

Share this post


Link to post
Share on other sites
constants mainly and some variables, but I don't think my path is being set.
Here's the config file :
[code]
<?php

    define('APP_DB', 'mri_sql');
    define('AUTH_DB_TBL', 'users');

define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
define('INTRANET_DIR', ROOT_PATH . '/database');
define('SCRIPTS', INTRANET_DIR . '/scripts');
define('CLASSES', INTRANET_DIR . '/classes');

define('LIBS', INTRANET_DIR . '/library');
define('PHPLIB_DIR', LIBS . '/php');
define('JS_LIB_PATH', LIBS . '/js');

define('PATH', CLASSES . ';' . SCRIPTS . ';' . LIBS);
ini_set( 'include_path', ';' . PATH . ';' . ini_get('include_path'));

define('TEMPLATE_DIR', INTRANET_DIR . '/templates');

require_once 'constants.php';
require_once $DEBUGGER_CLASS;
// require_once $ERROR_HANDLER_CLASS;
// require_once $USER_CLASS;
// require_once $TEMPLATE_CLASS;

//  And my database connection
?>
[/code]

But I just wanted to know that if was included once already shoud it be available accross the board?

Share this post


Link to post
Share on other sites
As an added query, to above.  If I have a function __construct() in my initial class, will it be run every time a child class extends it?  And is there a way of counting how many times a class is included/run?

Share this post


Link to post
Share on other sites
you have to specify parent::__construct() if you want child classes to use the parent's constrcutor.
[code]<?php

class A extends B
{
    public function __construct()
    {
        parent::__construct();
    }
}

?>[/code]

as for the include, it depends where you include it.

Providing you include it pre-object instantiation you'll be ok.

[code]<?php

require_once 'config.php';

$object = new MyClass($config);

?>[/code]

but what I mean by scope, is that if you declare a variable and try to use within a class (without global keyword, or passing as a parameter) it will not access it.

[code]<?php

class A
{
    private $foo;
    public function __construct()
    {
        $this->foo = $foo; // will not work.
    }
}

$foo = 'bar';

$obj = new A;

?>[/code]

[/code]

Share this post


Link to post
Share on other sites
Cheers Jenk, I think I've been following those rules, I just need to decipher now why my __consruct() is being run twice.  Any ideas as to what may be the cause.  I don't specifically call that function anywhere.  Just run NewClass = new Class;  And have another file being called that extends Class anything else do you know what what could do it?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.