note 43837 added to ref.classobj
| From: | greg at doutromundo dot com | Date: | Mon, 05 Jul 2004 23:58:08 +0000 |
| Subject: | note 43837 added to ref.classobj | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72636@lists.php.net to get a copy of this message | ||
As programmers, you're probably more organized than me, but, I do try and maintain some order
in my classes and codes and separate them in "packages" as in java.
This helped me keep them organized but caused havok when trying to use them, so what I did was to
create a class that handles the loading of classes (which I instanciate in all pages) along with my
error handling class all bundled up. This way, I can load my classes with a command similar to
$baseClass->loadClass("package","className"[,"constructor"]);
the function responsible for this has some checking to see if they are loaded and stuff like that...
function loadClass($packageName,$className,$constructor=""){
// if you dont have a constructor declare any function inside
// the class
if ($constructor==""){
$constructor=$className;
}
if(!is_callable(array($className,$constructor))){
if (defined("CLASS_DIR")){
$pkg = CLASS_DIR.$packageName."/";
if (is_dir($pkg)){
// we have a directory with the package name
$cls = $pkg.$className.".class.php";
if(is_file($cls)){
// we have a file
include_once($cls);
}else{
die("Class <b>$className</b> could not be found in package
<b>$packageName</b> , please check your instalation");
}
}else{
die("Package <b>$packageName</b> could not be found, please check your
instalation");
}
}
}
}
Just remember to define CLASS_DIR as the physical path for the directories where you packages are...
Hope this comes in handy...
Here's an example of a diretory strucutre...
/var/www/classes/ <- this would be CLASS_DIR
in there I have:
package1/
name.class.php
name2.class.php
....
The loadClass would look like: loadClass("package1","name");
Cute and easy
----
Manual Page -- http://www.php.net/manual/en/ref.classobj.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43837
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43837&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43837&report=yes
Search -- http://master.php.net/manage/user-notes.php