Posts Tagged ‘zend framework’

PHP Wishes 2012 – Interfaces and Standard Web Application Project Layout

Since Santa is listening, below are my PHP wishes for 2012:

  1. Standard Interfaces like Java which are used by many projects so that developers and architects focus on implementation
  2. Standardized Web Application Project Layout just like J2EE Web Application Directory Structure (http://java.sun.com/blueprints/code/projectconventions.html) or Linux Standard Base (http://en.wikipedia.org/wiki/Linux_Standard_Base)

I am learning Symfony 2 over the next coupla weeks, and though I am already familiar with Zend Framework 1, I am looking forward to Zend Framework 2 performance enhancements.

The question is that why is it so hard to get consensus for the betterment of all developers and aiding the momentum of the language forward?

Advertisement

Zend Framework Hidden Gems – Zend_Measure

Just had a bit of a problem today dealing with displaying file sizes of uploaded reports in an application we are upgrading using Zend Framework. When the files are uploaded, the file sizes are stored in bytes, but for display we need to show the sized in KB as the typical file size is between 60k and 2,048KB (2MB).

Looking through http://php.net I came across some scripts that we could use, but I was looking for a versatile re-useable solution. Well that is when I came across Zend_Measure (http://www.framework.zend.com/manual/en/zend.measure.html).

How did we use it, well, very simply:

  1. Added Zend_Measure library files into our application library folder (we do not install all components by default, we add them as we need to)
  2. Created a new Zend_Measure_Binary (http://www.framework.zend.com/manual/en/zend.measure.types.html) class instance
  3. Add the filesize in bytes
  4. Change the type from bytes to kilobytes (the conversion types are available as constants)
  5. Call the toString() method to output the formatted type

Now we have a reusable element which can be used …

Interfacing in the PHP World – Dictator Style

Seems that I have a radical approach to the need for PHP interfacing as suggested by Lucas Smith (http://pooteeweet.org/blog/2008/) then a rebuttal by Herman Radtke (http://www.hermanradtke.com/blog/please-do-not-interface-the-php-world/) and finally Lucas Smith again (http://pooteeweet.org/blog/2014)

I think that the approach for PHP should be:

1) Interfaces in the main PHP tree therefore all have to abide or die, that is how Java gets its notoriety. The SPL was a good start, so now let us spread the love to the rest of the core

2) Else start something like Apache Commons for Java which is the Switzerland, and allows drag and drop or reference implementations. However this is also difficult if there are no common interfaces

3) PHP needs that kind of dictatorship that Java has in order to move forward – dictatorships move things forward (whether good or bad).

Why do I say so?

1. What is an interface – it is a contract for behavior, which means that irrespective of the implementation you will expect the same behavior, which enhances encapsulation and abstraction core object oriented design principles. This is critical as PHP needs to grow past its roots and become truly enterprise.

2. PHP has grown successful because it is simple, but with simplicity comes a price which affects how the future looms. While PHP does not need a JCP like community process (3 – 5 years for Standards), it needs to have some form of commitees which advance different parts and aspects of the language which is now larger than the individual core developers. The interfaces mean that there is a reference implementation which is always followed by big vendor implementations – what PHP needs now are stable processes and standards for adding and managing features.

3. So how does this go, you cannot interface the whole language at a Go, however PHP’s object and procedural classes means that we gotta start from somewhere. And please do not go overboard, keep the interfaces simple and clean … I know there are issues like big vendor interests but they will have to sit together and work things out otherwise you will end up with people who will innovate over the language see Java’s Spring Framework (http://www.springsource.org/)

4. Using the HTTP request/response classes I have only used the Zend Framework versions and not the Symfony 2 versions yet (in a few weeks), however I like the object oriented nature of those … but that is just me 🙂

In closing, we have to learn to pick the best parts of other languages in order to improve whatever we use .

Year End New Challenge – Symfony 2

Well a new month December the last one of the year, and I am tackling a new challenge Symfony 2 (http://www.symfony-project.org/) For client projects we are using Zend Framework 1.11 + Doctrine 1.2 + JQuery which all run on PHP 5.2

I think the coming holidays give me a chance to jump into PHP 5.3+ along with Symfony 2 + Doctrine 2 + the latest PHP unit and dabble into Continuous Integration either Jenkins or Travis.

Zend_Navigation Render the SubMenu for Active Menu Item using a Partial

One of the biggest problems with Zend_Navigation helpers is using partials to render sub-menus in order to achieve customized rendering especially when additional properties have been defined.

The approach to the solution is as follows:


// find the first active menu item
$subnav = $this->navigation()->menu()->findOneByActive(1);
// The finder methods return null for no matches so nee to check that
if ($subnav != null) {
// render the matched menu using a partial
echo $this->navigation()->menu()->renderPartial($subnav, 'index/customsubmenu.phtml');
}

%d bloggers like this: