Is is possible, or even good practice to have includes inside a class definition?
- It’s possible.
- Not sure if it’s considered good practice, but I don’t see any problems with using include() within a class.
I use INCLUDE inside my classes as well and IMO provided they’re there not to INCLUDE other classes it is okay;
Remember though the more INCLUDEs you have then the more overhead your scripts have…
“inside a class definition”
You mean like this?
<?php
class MyClass
{
include 'MyMethods.php';
}
?>
That is not possible. But this is:
<?php
class MyClass
{
function a ()
{
include 'FunctionB.php';
b ();
}
}
$o = new MyClass;
$o->a();
?>
But I think this is very bad practice. You should use inheritance, composition and aggregation for this kind of things.
Well yes that could be another alternative I suppose but IMO I find it a lot easier to use INCLUDE… although personally I don’t INCLUDE other classes which I’ve already said…
What about lazy loading with a FactoryObject?
class Factory
{
function &Build($which)
{
if (!$this->_IsValid($which)) {
$which = DEFAULT;
}
if (!is_class($which)) {
require_once PATH.$which.'.php';
}
$ret =& new $which;
return $ret;
}
function _IsValid($which)
{
//some checks
}
}
I think that’s excellent practice.
I know some coding guidelines that tell you to include all classes and libraries at the top of the file, but this can lead to considerable parsing overhead if you include big files that you don’t use later. (Some scripts include Smarty and Adodb by default on every page.)
I always try to “require_once” classes at the latest possible moment, i.e. on the line right before the first “new” statement. This way you also avoid having to scroll back up to check if the class is already included.
Umm… I tend to INCLUDE_ONCE all the base files that hold the most common classes I know I will need:
# index.php
$fp = @fopen(DOC_ROOT_DIR .'includes.protected', 'r');
$line = '';
while(!@feof($fp)) {
# read file...
$line .= str_replace("\\r\
", '', @fgets($fp, 256) /* ...at 256 bytes */ );
}
@fclose($fp);
$installation = explode(';', $line);
$files = explode(',', $installation[0]);
$classes = explode(',', $installation[1]);
# script installation
for($num = 0;$num < sizeof($files);$num++) {
$getFile = (string) $files[$num];
$getClass = (string) $classes[$num];
#
@include_once(LIB_ROOT_DIR . $getFile);
if(!class_exists($getClass)) {
$component = explode('.', $getClass);
# error with a file inclusion
trigger_error('error :: failed to install component: '. $component[0], E_USER_WARNING);
return false;
}
}
And then it’s a simple case of just INCLUDing the VIEWs… ?
Including inside a class is horrible, horrible coding.
How would you propose NOT loading all the posible concrete factory classes other than the one being build?
I would say “use a compiled language”, but that might get me killed in here, so I’ll say, no, it’s not possible. In the case of a factory object, it makes sense. (Should have read the thread more carefully)
But for the record, I still hate this:
<?php
class MyClass
{
include 'MyMethods.php';
}
?>
Which results in a PHP syntax error anyway. (I tried it seeing if I could fake double inheritance once).
I would say “use a compiled language”, but that might get me killed in here…
Very very true…