Jump to content

Classes logic more help needed please ;)


scottybwoy

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
Link to comment
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?
Link to comment
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]
Link to comment
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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.