Jump to content


Photo

Classes logic more help needed please ;)


  • Please log in to reply
5 replies to this topic

#1 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 29 September 2006 - 09:46 AM

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 :

<?php

require_once 'config.inc.php';

class PHPApplication {
    ....
}
?>

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

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 29 September 2006 - 10:04 AM

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?

#3 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 29 September 2006 - 11:23 AM

constants mainly and some variables, but I don't think my path is being set.
Here's the config file :
<?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
?>

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


#4 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 29 September 2006 - 02:21 PM

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?


#5 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 29 September 2006 - 06:28 PM

you have to specify parent::__construct() if you want child classes to use the parent's constrcutor.
<?php

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

?>

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

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

<?php

require_once 'config.php';

$object = new MyClass($config);

?>

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.

<?php

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

$foo = 'bar';

$obj = new A;

?>

[/code]

#6 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 03 October 2006 - 04:35 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users