note 86195 modified in language.oop5.autoload by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Because static classes have no constructor I use this to initialize such classes.
The function init will (if available) be called when you first use the class.
The class must not be included before, otherwise the init-function wont be called as autoloading is not used.

<?php
function __autoload($class_name)
{
	require_once(CLASSES_PATH.$class_name.'.cls.php');
	if(method_exists($class_name,'init'))
		call_user_func(array($class_name,'init'));
	return true;
}
?>

I use it for example to establish the mysql-connection on demand.

It is also possilbe do add a destructor by adding this lines to the function:
<?php
if(method_exists($class_name,'destruct'))
	register_shutdown_function(array($class_name,'destruct'));
?>

--was--
Because static classes have no constructor I use this to initialize such classes.
The function init will (if available) be called when you first use the class.
The class must not be included before, otherwise the init-function wont be called as autoloading is not used.

<?php
function __autoload($class_name)
{
	require_once(CLASSES_PATH.$class_name.'.cls.php');
	if(method_exists($class_name,'init'))
		call_user_func(array($class_name,'init'));
	return true;
}
?>

I use it for example to establish the mysql-connection on demand.

It is also possilbe do add a destructor by adding this lines to the function:
<?php
if(method_exists($class_name,'destruct'))
	call_user_func(array($class_name,'destruct'));
?>

http://php.net/manual/en/language.oop5.autoload.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.