Tag: Zend Framework

Method chaining with PHP 5

Since the introduction of PHP 5 and it’s better OO capabilities it is possible to use method chaining. In most modern frameworks, like Zend Framework‘s Zend_Mail for example, it’s widely used.

An example of method chaining from the Zend Framework documentation for the Zend_Mail component looks like this:

$mail = new Zend_Mail();
$mail->setBodyText('This is the content of the mail.')
     ->setFrom('somebody@example.com', 'Some Sender')
     ->addTo('somebody_else@example.com', 'Some Recipient')
     ->setSubject('My own subject')
     ->send();

The code above can also be written like below, without method chaining:

$mail = new Zend_Mail();
$mail->setBodyText('This is the content of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('My own subject');
$mail->send();

It depends on your likings of which method you want to use, but you can extend your methods with the chaining way without harming the traditional way so in the end the programmer can decide the method he desires. If you want to make the methods ready for it the method simply needs to return the object itself, like so:

public function foo() {
    // ... do something here ...
    return $this;
}

I’ve highlighted line 3 where the magic applies. If you had foo(), bar(), baz() and bat() methods you could then do this:

$myClass->foo()->bar()->baz()->bat();

You may not want or need to do this in any of your code, but the possibility is there if you or any colleague wants to.


Getting started with the Zend Framework

I’ve started playing with the Zend Framework and noticed that there were lots of getting started tutorials for the old versions. Since many of them don’t update or work anymore I’ve decided to write my own getting started version which should get you up to speed with Zend Framework version 1.0.1 in no time.

At first you need to download and install the framework in a seperate directory, you can either choose that it’s available in the include_path using the php.ini file or by setting it up in a folder which will be used only for your application. If you go for the first one edit the php.ini and search for the include_path directive, add the complete path to the library directory from the extracted Zend Framework archive and you should be set, don’t forget to reload Apache!

The second method is slightly harder but it should also be possible when you’re on a shared hosting platform and the provider hasn’t got the framework installed. Extract the archive somewhere outside the web root (this means that it shouldn’t be accessible through the website).

When you set up a new project, the following directory structure is recommended:

- app
    - controllers
    - views
        - scripts
- library (contains the Zend Framework files)
- public_html (or htdocs in other cases - this is the web root)

In the public_html directory create a file called index.php. This will be our bootstrap for the framework. But first we need to get the PHP error reporting to a decent level and make sure to have access to the framework files. Open index.php and add the following; (continue reading…)


Copyright © 1996-2010 Re:morse.nl. All rights reserved.
iDream theme by Templates Next | Powered by WordPress