Framework design questions

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Framework design questions

Post by Ward »

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;
}
And usage would be something like this:

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]
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

cool function :)

You could make use of array_pop() and change to:

Code: Select all

$name = array_pop($namespace);
Then change the loop to:

Code: Select all

foreach ($namespace as $space) { 
                        $path .= $space . "/"; 
                }
and also for pure fussyness, I would use:

Code: Select all

if ($path = realpath($path)) {
    include $path;
}
Merely observational comments, not criticism or anything that could cause offence, start wars, or tick people off :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you might consider upgrading to php5, and using the __autoload function :wink:
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

If you take a look a the current practice (things like the Zend Framework) they usually will load the class named "This_Is_My_Class" from the file "This/Is/My/Class.php". Which means they don't just use the last word in the "namespaced" name passed.
(#10850)
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

i love my sometimes funtional auto_loader. i have to go back and fix it. it's to get all my libraries and easilly use them

Jcart, what does autoLoad do?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

when in doubt, read the manual.. http://ca3.php.net/__autoload

In a nutshell, this function will be called whenever you create a new object. This function will be in charge of finding the file and loading it into memory.

ie..

Code: Select all

function __autoload($class)
{
   require_once('modules/'.$class.'.php');
}
You can loop directories and emulate a search using file_exists() aswell.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

__autoload()

EDIT: beaten..
Post Reply