Blog entries tagged with 'php'
-
Zend Framework Released
The much awaited Zend Framework was released! -
ActiveRecord PHP implementation Part I
This is the first part of an attempt to implement the ActiveRecord design pattern in PHP. This first implementation is very simple but will open doors for more advanced stuff, like foreign keys mapping(automatic or manually) or integration with Model, View and Controller components, in this case with the generation of PHP classes or objects. Approach: A row in a database table or view is wrapped into a class, thus an object instance is tied to a single row in the database. Imagine this table(person):+-------------+---------------------+ | Field | Type | +-------------+---------------------+ | id | bigint(20) unsigned | | name | varchar(100) | | email | varchar(150) | | last_update | datetime | +-------------+---------------------+
With this records:+----+----------+---------------+---------------------+ | id | name | email | last_update | +----+----------+---------------+---------------------+ | 1 | Nuno | nuno@xxx.xxx | 2005-11-06 20:39:08 | +----+----------+---------------+---------------------+ | 2 | Marta | marta@xxx.xxx | 2005-11-06 20:39:08 | +----+----------+---------------+---------------------+ | 3 | Pedro | joao@xxx.xxx | 2005-11-06 20:39:08 | +----+----------+---------------+---------------------+
You don't have to define any table structure, just extend de ActiveRecord class and call the parent __construct() method with the table name :include_once 'class.ActiveRecord.php'; class Person extends ActiveRecord { public function __construct() { parent::__construct('person'); } }
To return all rows in the table:$persons = new Person(); /** * This will print all records * $persons->findAll() will return a Recordset */ foreach($persons->findAll() as $person) { echo 'id: '.$person->id; echo 'name: '.$person->name; echo 'email: '.$person->email; echo 'last update: '.$person->last_update; } /** * Alternatively you can 'walk' the Recordset * with next(), prev(), etc. because Recordset * implements the Iterator interface */
Insert a record:$person = new Person(); $person->name = 'Julia'; $person->email = 'julia@xxx.xxx'; $person->insert();
Update a record:$personId = $_POST['id']; $person = new Person(); $person->id = $personId; $person->name = 'Nuno Mariz'; $person->update();
UPDATE: With the Pádraic Brady tip for merging the insert() and update() methods to only one save() method, now is possible to:// insert $person = new Person(); $person->name = 'Nuno Mariz'; $person->save(); // or update $personId = $_POST['id']; $person = new Person(); $person->find($personId); $person->name = 'Nuno Mariz'; $person->save();
You can view the UML for the implementation. See you in part II... -
Zend Framework Webcast
php|architect has posted the recorded version of their Zend Framework webcast on their site. I must say is quite impressive. I've been developing a new framework to take advantage of the new features of PHP5. Right now is quite mature and is in prodution, but lacks of some features. One of the features that is missing is the ActiveRecord that Zend will implement in their new framework. Quite curious is the ZMail, have almost the same API of my Mailer class. One of the aspects that im also curious is the ZForms, right now in my framework is quite flexible. I can extend my FormManager class and add the elements that i need, like this:include_once 'class.FormManager.php'; class FormLogin extends FormManager { public function __construct() { /* * Calling the parent * Usage:: * parent::__construct($name, * $action = null, * $method = null, * $enctype = null, * $extra = null); */ parent::__construct('FormLogin', null, 'POST', null, null); } // Must implement this method public function initElements() { /* * Adding an Hidden Element * Usage: * $this->addHiddenElement($name, $value); */ $this->addHiddenElement('module', 'Authentication'); $this->addHiddenElement('action', 'login'); /* * Adding an Element * Usage: * $this->addElement($type, $name, $label, $options); */ $usernameOptions = array(...) $this->addElement('text', 'username', 'Username', $usernameOptions); /* * Adding a Validator * Usage: * $this->addValidator($elementName, $validatorObject); */ $this->addValidator('username', new RequiredValidator()); $this->addValidator('username', new MaxLengthValidator(array('length'=>15))); $passwordOptions = array(...) $this->addElement('password', 'password', 'Password', $usernameOptions); $loginOptions = array(...); $this->addElement('submit', 'login', 'Login', $loginOptions); } } $form = new FormLogin(); $form->render(); // render HTML form echo $form->getFormStart(); // show the Username label echo $form->getElementLabel('username'); // show the Username imput echo $form->getElementInput('username'); // show the Password label echo $form->getElementLabel('password'); // show the Password input echo $form->getElementInput('password'); // show the Login submit button echo $form->getElementInput('login'); // show the Login submit button echo $form->getFormEnd();
Another way is to load the XML configuration for the form:include_once 'class.XMLFormManager.php'; $form = new XMLFormManager('FormLogin.xml'); $form->render(); // render HTML form echo $form->getFormStart(); echo $form->getElementLabel('username'); // show the Username label echo $form->getElementInput('username'); // show the Username imput echo $form->getElementLabel('password'); // show the Password label echo $form->getElementInput('password'); // show the Password input echo $form->getElementInput('login'); // show the Login submit button echo $form->getFormEnd();
In the XML way is quicker, i have a web application to generate the XML automatically. I can create or load XML forms and edit with an interface. I can bind elements with database table fields, add and configure validators, etc. Recently i've improved the package to support the binding of an element with a database table field, the syntax is similary but with 2 more parameters in the addElement() method. -
PHP Collaboration Project
WOW. Im already anxious to see this working. One thing that think that is missing in PHP, is the existence of an web application framework or an Application Server, which standardizes the way PHP applications are built.What is the PHP Collaboration Project?
The PHP Collaboration Project is an open source initiative through which the PHP community and partners will create an industrial-grade PHP Web application development and deployment environment...
Zend PHP Framework
By Joel's Corner
A Web application framework which standardizes the way PHP applications are built. The Zend PHP Framework accelerates and improves the development and deployment of mission-critical PHP Web applications. -
"Submitting a bug to PHP" - The novel
This is my first(and the last) time of an attempt to submit a bug to PHP. I am using PDO in PHP5 and my Apache gets a "Segmentation Fault" when I execute an invalid SQL statement.[26 Aug 8:02pm CEST] nmariz at estadias dot com Description: ------------ Segmentation fault in Apache when executing an invalid SQL. apache error.log: [Fri Aug 26 18:10:42 2005] [notice] child pid 26519 exit signal Segmentation fault (11) Actual result: -------------- Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 1079565216 (LWP 28567)] 0x40865cf5 in _efree (ptr=0x83bbf58) at /root/software/php-5.1.0RC1/Zend/zend_alloc.c:302 302 REMOVE_POINTER_FROM_LIST(p); (gdb) bt #0 0x40865cf5 in _efree (ptr=0x83bbf58) at /root/software/php-5.1.0RC1/Zend/zend_alloc.c:302 #1 0x40727109 in pdo_mysql_stmt_dtor (stmt=0x0, tsrm_ls=0x817ecf8) at /root/software/php-5.1.0RC1/ext/pdo_mysql/mysql_statement.c:45 (...) [26 Aug 9:29pm CEST] xxx@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with < ?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If possible, make the script source available online and provide an URL to it here. Try to avoid embedding huge scripts into the report. [29 Aug 10:42am CEST] nmariz at estadias dot com Segmentation fault when executing an invalid SQL statement: < ?php (...) // invalid sql statement $query = 'SELECT id name,text FROM modules'; $stmt = $this->dbconn->query($query); $result = $stmt->fetchAll(); $stmt = null; return $result (...) ?> [29 Aug 1:24pm CEST] xxx@php.net What part in "a short but complete example script" did you not understand? [29 Aug 5:00pm CEST] nmariz at estadias dot com What part in "Segmentation fault when executing an invalid SQL(_____ANY_____) statement" did you not understand? I think that you don't need more code to understand what i mean. [29 Aug 5:07pm CEST] xxx@php.net I think you don't understand that I am very very lazy. And that your problem _might_ be somewhere ELSE than in the query part. So either give the full code or go away.
Unbelievable!
UPDATE: After i had submitted the code that he asked, the guy simply erased all logs and submitted this:[29 Aug 11:34pm CEST] xxx@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with < ?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If possible, make the script source available online and provide an URL to it here. Try to avoid embedding huge scripts into the report.
You can see the log in here. -
The PHP native template engine
“Why using templates engines if PHP is itself a template engine?” said Karlus some time ago. The answer is: The templates engines can be a great help when you're using a PHP framework that is built on modules(using MVC design pattern) where is necessary to fetch pieces of HTML to build a page, separating all code of PHP from HTML.
But the overhead of this templates engines is enormous, because of the all parsing envolved. So templates engines is not all about a sea of roses.
I have used Smarty for this kind of work for a long time and, in my opinion, is the best.
When i read this article i realized Karlus point of view. So based on this idea i have developed a small class that enables all funcionality that Smarty has (witch i use) but using the PHP natively as the parser.
It's a class with only 160 lines of code(without comments), so don't expect too much...
Features:
Using templates that are PHP files
Including templates inside templates
Caching with expired system in seconds or without expiration
Caching with id's
Plugin system, as simple as you can get
Discover the diferences with a simple template:Smarty template (index.tpl)
<html> <body> {include file="header.tpl"} Title: {$title}
List:
{foreach from=$list item=list_item} {$list_item}
{/foreach} </body> </htm>Smarty object creation (index.php)
require_once('lib/smarty/Smarty.class.php'); $smarty =& new Smarty(); $smarty->template_dir = 'smarty/templates/'; $smarty->compile_dir = 'smarty/templates_c/'; $smarty->cache_dir = 'smarty/cache/'; $smarty->caching = 1; $smarty->assign('title', 'My Title'); $smarty->assign('intro', 'The intro paragraph.'); $smarty->assign('list', array('cat', 'dog', 'mouse')); $smarty->fetch('index.tpl');
My Template (index.tpl)
<html> <body> <?=$this->includeTemplate("header.tpl");?> Title: <?=$title;?> <br /> List: <br /> <?php foreach ($list as $item):?> <?=$item;?> <br /> <?php endforeach; ?> <html> <body>
My Template object creation (index.php)
require_once('lib/Template.php'); $tpl =& new Template(); $tpl->setTemplatesPath('templates/'); $tpl->setCache(true); $tpl->setCachePath('cache/'); $tpl->setCacheId('nuno'); $tpl->setExpire(0); $tpl->assign('title', 'My Title'); $tpl->assign('intro', 'The intro paragraph.'); $tpl->assign('list', array('cat', 'dog', 'mouse')); $tpl->fetch('index.tpl');
Results
The time results shown below, were measured with average results to have a more accurate benchmark.
The benchmarking was made in a Celeron 2400Mhz with 512Mb of RAM.
The Y axis corresponds to the average time in seconds per request and the X axis to the number of requests.
Download
You can download the source code here. -
Oracle Launches new PHP Developer section on the OTN
After one of the major database company (SAP recently launched a new PHP Development Forum), Oracle launches new PHP Developer section on the OTN.
Among other things you can read this interesting guides:Installing Apache, PHP, JDeveloper, and the PHP Extension on Linux
Installing PHP and the Oracle 10g Instant Client for Linux and Windows
Installing Oracle, PHP, and Apache on Linux
Installing Oracle, PHP, and Apache on Windows 2000/XP
Set Up Oracle Database 10g and PHP on Mac OS X
Is great to see that large companies are becoming aware that PHP has moved from a thriving open source project to an enterprise solution for web applications.