Framework Components

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
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Framework Components

Post by Syntac »

What components do you consider absolutely essential for a good framework? (More specifically, models and views.)
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Framework Components

Post by Theory? »

Authentication, database interaction, some sort of generic CRUD functionality maybe?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Framework Components

Post by Christopher »

Syntac wrote:What components do you consider absolutely essential for a good framework? (More specifically, models and views.)
That could be a very long list if you actually want to itemize the many different things needed for a web app framework! Are you looking for the major functionality for models, views and controllers?
(#10850)
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Framework Components

Post by Syntac »

Just models and views at the moment. I'm going for a "bare-bones" sort of list here. You know, CRUD functionality, HTML generation, data validation, and the like.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Framework Components

Post by Christopher »

Syntac wrote:CRUD functionality,
Table Data Gateway is a good start
Syntac wrote:HTML generation,
Templates or OO SQL
Syntac wrote:data validation,
Validators (and Filters too)
Syntac wrote:and the like.
it's a long list if you want to get at all specific.
(#10850)
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Framework Components

Post by The_Anomaly »

Pagination is really sweet when it's done right.

The following is just something that I thought of when I was irritated with writing domain objects for tables in my database. In my other thread, we have the Note, which has a bunch of properties, say, 20. I make the table in the database first, and then I sit down for half an hour and basically just copy all of the properties, and make standard getters and setters for each one. It's a real pain.

Could there be some framework component that would get the names of all of the columns, and then create a domain object with each of the columns as properties, and then a getter and setter for each?

EDIT: Ugh, I just read "Absolutely necessary," in the OP. Feel free to ignore this post, as it's most definitely not "Absolutely necessary," but still, I'd use it literally all the time.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Framework Components

Post by Weirdan »

The_Anomaly wrote:Could there be some framework component that would get the names of all of the columns, and then create a domain object with each of the columns as properties, and then a getter and setter for each?
Isn't it easy to create yourself? I mean it's not that hard, really:

Code: Select all

 
#!/usr/bin/php
function help() {
  global $argv;
  return <<<EOF
Usage: $argv[0] <TableName> > class.php
EOF;
}
if ($argc < 2) {
  die(help());
}
 
$tableName = $argv[1];
mysql_connect('localhost', 'root', '');
mysql_select_db('database');
 
$classDesc = new stdClass;
$classDesc->className = $tableName;
$classDesc->properties = array();
$fieldsResult = mysql_list_fields('database', $tableName);
for ($i = 0, $l = mysql_num_rows($fieldResult); $i < $l; $i++) {
  $classDesc->properties[] = mysql_field_name($fieldsResult, $i);
}
 
echo 'class ' . $classDesc->className . ' extends BaseObject {' . "\n"; // class opening
 
// properties
foreach ($classDesc->properties as $property) {
  echo 'protected ' . $property . ' = null;' . "\n";
}
 
// setters and getters
foreach ($classDesc->properties as $property) {
  echo 'public function get' . ucfirst($property) . '() {' . "\n";
  echo '    return $this->' . $property . ';' . "\n";
  echo '}' . "\n";
  echo 'public function set' . ucfirst($property) . '($value) {' . "\n";
  echo '    $this->' . $property . ' = $value;' . "\n";
  echo '}' . "\n";
}
 
// class closing
echo '}' . "\n";
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Framework Components

Post by josh »

active record with meta data
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Framework Components

Post by Luke »

Well, absolutely necessary is really a matter of opinion. To me, absolutely necessary means, if it the framework doesn't have it, I would use something else. Here are things I find necessary when building an application:

View:
Template system - this doesn't have to be a whole template engine or anything, just need two things. First, layouts. I need to be able to specify a layout for every view that is rendered. Second, I need to be able to set conext variables for every view. I just use the template syntax in php for view scripts

Model:
DAO - a simple class with CRUD methods for a given table/model
Model - a class that uses the DAO to create more abstract methods such as "User::createAdmin()", "Article::createDraft()", "Article::getAllWithinDateRange($datestart, $dateend)" etc.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Framework Components

Post by Syntac »

I think I've implemented all of that. Partayyyy! :D
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Framework Components

Post by josh »

The components aren't as important as what each can do. Your controller should support:

template method for hooking controller construction
plugin / "filter" support
integration with view object
forwarding
Various methods of routing in the FC to the controller, including regex routes, and ideally support "backwards URL construction" where I can generate URLs with parameters, using the router - to centralize URL handling

These are just some more things I consider "essential"

Other cool things would be "out of the box" plugins / action helpers like action Stack, view rendering, ACL + auth support & integration, etc
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: Framework Components

Post by The_Anomaly »

Weirdan wrote:
The_Anomaly wrote:Could there be some framework component that would get the names of all of the columns, and then create a domain object with each of the columns as properties, and then a getter and setter for each?
Isn't it easy to create yourself? I mean it's not that hard, really:

Code: Select all

 
#!/usr/bin/php
function help() {
  global $argv;
  return <<<EOF
Usage: $argv[0] <TableName> > class.php
EOF;
}
if ($argc < 2) {
  die(help());
}
 
$tableName = $argv[1];
mysql_connect('localhost', 'root', '');
mysql_select_db('database');
 
$classDesc = new stdClass;
$classDesc->className = $tableName;
$classDesc->properties = array();
$fieldsResult = mysql_list_fields('database', $tableName);
for ($i = 0, $l = mysql_num_rows($fieldResult); $i < $l; $i++) {
  $classDesc->properties[] = mysql_field_name($fieldsResult, $i);
}
 
echo 'class ' . $classDesc->className . ' extends BaseObject {' . "\n"; // class opening
 
// properties
foreach ($classDesc->properties as $property) {
  echo 'protected ' . $property . ' = null;' . "\n";
}
 
// setters and getters
foreach ($classDesc->properties as $property) {
  echo 'public function get' . ucfirst($property) . '() {' . "\n";
  echo '    return $this->' . $property . ';' . "\n";
  echo '}' . "\n";
  echo 'public function set' . ucfirst($property) . '($value) {' . "\n";
  echo '    $this->' . $property . ' = $value;' . "\n";
  echo '}' . "\n";
}
 
// class closing
echo '}' . "\n";
 
Sweet. Thanks for throwing that together--you're right, I should have thought to do it myself.
Post Reply