Framework design questions
Posted: Thu Jul 13, 2006 10:12 am
Pimptastic | Please use
And usage would be something like this:
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I'm currently building my own framework, which should help me create projects faster in the future. Currently it's not so much a framework, but rather a collection of objects that make tedious tasks simpler. For example, I have classes for HTML form inputs, such as select, textbox, etc. I have another class for MySQL, which handles the connection, and all queries. However, as I create more, I realize the need for some kind of automatic include system, so I dont need 100 include statements at the top of my page. My solution was a custom object init function. Instead of using 'new', I would use this function to create a new object. It will first check if the class is defined, and include it if needed. I am using namespaces, which are really just file paths, separated by periods. For example, "html.forms.select" would refer to "/html/forms/select.class.php".
Here is my function:
(I realize that the arguments var will only pass one argument, i'm working on that)Code: Select all
function create_new($namespace, $arguments="")
{
$namespace = explode(".",$namespace);
$name = $namespace[count($namespace)-1];
$path = DOMINO_BASE;
if (!class_exists($name))
{
for ($i = 0; $i < (count($namespace)-1); $i++) {
$path .= $namespace[$i]."/";
}
$path .= $name.".class.php";
if (file_exists($path))
{
include $path;
}
else
{
common::error("Could not find '$path'");
}
}
$object = new $name($arguments);
return $object;
}Code: Select all
$select = create_new("html.forms.select");
$select->add_option("Option 1","1");
$select->add_option("Option 2","2");
$select->value = "2";
$select->display();Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]