PHP Framework and ORM vs Stored Procedures - php

I am in the process of picking a PHP framework for a web application I am starting. I have never really used a framework in the past but with this project there is a great need.
I have been debating between the usual suspects; CakePHP, Zend Framework and Symfony. I have been going back and forth about which framework will work best for me and this project. I am leaning towards CakePHP but I am still researching.
My question is not what framework is best. I know there is no real answer to that and there are tons of posts related to this subject. My question is related to the Model and ORM. I have read a lot about ORMs being slow and I am concerned about speed. I am very comfortable writing SQL and in the past have tried to keep all of my database interactions in stored procedures.
I am looking for some feedback about using CakePHP's ORM or Doctrine with Zend or Symfony as apposed to keeping everything in stored procedures. I know stored procedures are going to be faster but what else will I loose if I do not use an ORM? I understand that an ORM will give me database abstraction but in my mind that just helps people who do not write SQL. I also know that I do not know enough about ORMs.
If anyone can give me some feedback about this and which framework might be best based on using or not using an ORM.
Thanks for any help in advance.

0) Bang for effort
The key advantage with an ORM is that you don't spend as much time dealing with the persistence layer. A pre-written ORM ( I have worked with EclipseLink) will provide a ton of things you probably won't get in custom written stored procs. I think it's worth thinking about how much time you want to spend writing your persistence layer.
1) Caching
All the major ORMs provide multi-level distributed caches. Combined with Named/Predefined queries you can get SQL queries that don't actually have to go to the database. This can give you excellent performance.
2) Abstraction
ORMs allow you to define your table layout in one location and then they manage all the painful mapping between columns/tables and objects. Some will allow you to remap column, table and schema names without changing any code at all. If you work with people who like to change things around this can really simplify things.
3) Speed
Some ORMs can have bad performance, but it really is based on how you use it. I find that you tend to end up over-querying for things. On the other hand, you get things like built-in query profilers. You can write custom SQL for queries if you find you aren't getting the performance you need.

Mark Robinson gives a great response. I'm just going to back up what he says by giving our experience with Doctrine2.
I chose to use Doctrine2 as our ORM with Zend Framework a little while back. Our project is still being developed, but choosing D2 has been a decision we've not regretted one bit. Whilst you still need to give a lot of thought to your data architecture, D2 gives you the flexibility to be able to modify that model at a later date if needs be. It allowed us to try things out quickly in the early stages and the room to grow and change later when we decided that things weren't quite right - it happens.
In relation to Mark's point about abstraction. One of the other things I love about D2 is that we're working with plain old PHP objects. Don't underestimate the power of being able to think in terms of objects - both for the people responsible for modelling the data and the developers who work with the data - it'll make your life easier, trust me. Also, having inline documentation of the ORM mapping (if you choose the docblock approach) is nice.
Right, performance. As Mark says, there are ways and means to speed things up - but there's always going to be some overhead. Whenever you introduce another software layer, there'll be some performance hit, but it's a tradeoff. For us, the tradeoff - the advantages of using the ORM vs performance - is worth it. We'd spend more time debugging code and not getting things done without the ORM.
Anyway, D2 can help you with caching for queries, results and metadata. Whilst you probably just want an array cache during development, it's great that the facility for things like APC, memcache etc. is there when you go to test and deploy. You could even develop your own if you're brave.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/caching.html
Hope that helps, I've probably missed stuff, but if you have any questions just fire them in and I'll do my best.

A framework implements mainly three kinds of features :
the flow between "getting a request" and "rendering a page". That's were you put things like MVC, router, etc...
the way to manage your model and it's persistence. That's where you see acronyms like ORM, DBAL, DAO
Components. Features, often working also standalone. Like Xml parsing, i18n handling, pdf generation...
When you choose your framework, it in facts means that you choose 1). It's the thing you will certainly have to stick with, it's the flow of your application. 2) and 3) ? You can integrate those you prefer. As an example, i'm on Zend Framework with most of it's components, but use Doctrine ORM 2 and Symfony's Dependency injection. A friend of mine is on Symfony 2, uses Doctrine ORM too, but does it's pdf generation and mail management with Zend's related components.
The other thing you need to know if that currently there is a "second generation" of php frameworks/orm's, (re)written to take advantage of the new php 5.3 features, and/or to solve the general performance/coupling issues they (nearly) all had. Some of them are production ready, some are still under development :
Doctrine ORM 2 (production ready)
Symfony 2 (production ready)
CakePhp 2 (in RC 2 currently, but by the time your project is ready it should be stable)
Zend Framework 2 (still under active development, but normally not for so long)
FLOW 3 (beta2, should be ready soon too)
For the ORM part, i'll recommend using one, especially Doctrine's. #Mark and #iainp999 explained perfectly why.

ORMs are for programmers who don't "grock" SQL!
Yes ORMs make it easier (or at least require less lines of code) to write simple CRUD stuff, but when you get to more complex requirements its like trying to write SQL with a piece of wet spaghetti from ten feet away.
So stick with SQL.
Its worth looking at something like "SQLMap" which is ORM starting from the "R"elational side of the mapping (most try to map an "O"bject on to a table). This will allow you to write the SQL yourself and generate the appropriate "helper" classes to easily access the results in your program.

Related

Scalable app design in php

I'm a beginner php developer who is trying to build a social network for my school students. Knowing that the school has over 1000 students who are already active, I must have a plan of expanding / scale the code that I write.
Earlier it was just the LAMP Stack, now the modern web development is way more than that as I see, I'm truly kind of lost in what technologies to use and how to incorporate them to build a scalable app. I'm hoping to divide this application into 3 layers.
Application layer (phalcon,reddis,apache,php)[mvc api centric]
Database layer(mysql)
UI layer - (html/css/js/)
This is where i need help, is this design approach good for a scalable app ? where can i improve ? any explanations, links for further reading will be a highly appreciated.
Welcome to SO. I cannot think of a particular reference guide to direct you to (although the PHP manual is a good place if you end up stuck with how to do something specific). I would suggest reading a bit of several results when you search "Getting started with MVC in PHP" and noting what they agree on. That said, take a look at what I say below (and then ignore it as much as you please ;) ).
Firstly, you are wiser than many in sorting out a scalable design before launching into the project...
I'm excited to see Phalcon in your list there already. However, as DevDonkey suggested, start with something simpler first (Phalcon is very powerful but to really get to grips with it you need a good grasp of PHP, particularly object-orientated programming).
If you are completely new to PHP...
... try building a small app (products table, view/add/edit/delete functionality) and learn the beginnings of the language that way, as this answer suggests. Things will go wrong and you'll discover lots of headaches when you want to change one feature and it affects everything else but that will help you to understand the importance of...
MVC design
From your question I can see you have at least heard of this. This is really where the layers of your application lie:
Model - interactions with the database (retrieving/editing data) are handled through this. So you could have a MYSQL database and then your models provide a nice interface to interact with the data (generally you have one model for each table).
View - this is the last layer, what the user sees. So you will make use of your html/css/js knowledge here. On this topic, unless you really want to do your own css consider using a CSS Framework such as Bootstrap. It will really help speed up making your site look good and there are loads of free templates out there to use with it.
Controller - this is the application logic. The controllers request/manipulate data through the models and then decide what to send to the views for rendering.
Use a framework?
Using a good framework can make your application more reliable and quicker to build. But using a framework without understanding it will be frustrating, slow and possibly result in worse code than if you didn't use one to begin with (as you employ hacks to get around the pieces of the framework you don't understand). My current favourite is Phalcon but as a relative beginner to PHP I would suggest something more like CakePHP although both Laravel and Symfony are also popular.
Summary
Start small, learn, test ideas out and then build up to a bigger project.
Get comfortable using PHP (including OOP style) before using a framework.
Use an MVC framework
The layers you laid out in your question are good, but I would split it slightly differently (considering that MVC is the 3 layers)
Application Layer - controllers, written in PHP, handles logic/manipulation, often the biggest layer
Database Layer - models, written in PHP, you will also need a database which could be in your favourite database language - MySQL ;)
UI Layer - views, possibly written in PHP (depending on the framework) but also HTML, CSS and JS as well as well as a templating language if you wish (e.g. Twig or Volt), essentially a way to make the response from the controller nice for a human
First Project (for CakePHP)
This blog tutorial is a good place to start if you decide to use CakePHP.
Getting started with Phalcon
Phalcon is more powerful/verstile, but to get started with it I feel you have to be a better PHP developer than you do to get started with something like CakePHP. Take your time to understand each new concept with Phalcon, particularly Dependency Injection.
Even having used CakePHP for the past 2 years and being familiar with MVC patterns and PHP, I still worked my way through all 7 of the tutorials in Phalcon.
Having said this, my favourite thing about Phalcon is that it is highly decoupled - so it is fairly easy (after a while) to replace bits of it with your own extensions if it doesn't quite do what you want.
Note about Phalcon: It is not as popular as many other frameworks (although popularity is growing) and so you may have to spend some time digging around when you get stuck. However, the docs are improving all the time and the forum is very active. Unfortunately the number answering questions about it on StackOverflow is still small compared to many other frameworks.

What is a good database model definition language?

We are developing a substantial PHP web application using the Zend framework.
At this time, the product is starting to stabilize and we are moving away from quick-and-dirty setups in order to avoid regressions. In particular, we are now working on top of a single shared database that everyone edits. We want to get rid of this ASAP.
The Zend framework does not seem to support the classic RAD (Djangoish) mechanism where you define your data models and then it creates the tables for you. So we are thinking of using an external ORM tool that will do this.
We could have our schemas and initial fixtures defined in plain SQL but this is a) verbose b) error prone c) too low level and d) problematic because we must maintain different versions for every supported database backend.
So we are thinking of using an ORM like Doctrine or Propel to define our models and create tables with their initial data using the chosen framework's dialect. The application uses the Zend tool for ORMing so consistency between both tools would have to be maintained manually, but since changes are more gradual now this doesn't seem like much of a problem.
So, far, we are evaluating Doctrine and Propel for this task. Any suggestions about other ORMs that we missed? Maybe a different approach altogether for the task at hand?
Thanks!
Gonzalo
I really love RedBean. You keep your database models in pure php and it keeps track of everything.
RedBeanPHP is an open source ORM tool
for PHP. It focuses on simplicity and
ease of use. What makes RedBean unique
is that it creates your database
schema on-the-fly. It scans your data
and adjusts the column types to fit
your object properties. If your models
are stabilized you can freeze the
database. This way RedBean is easy to
develop with but is also extremely
fast on production servers.
Since I've found this ORM, I don't use doctrine any more.
http://redbeanphp.com/

ORM or SQL in large, scalable and MAINTAINABLE web application?

I know there already are a lot of posts floating on the web regarding this topic.
However, many people tend to focus on different things when talking about it. My main goal is to create a scalable web application that is easy to maintain. Speed to develop and maintain is far more appreciated BY ME than raw performance (or i could have used Java instead).
This is because i have noticed that when a project grows in code size, you must have maintainable code. When I first wrote my application in the procedural way, and without any framework it became a nightmare only after 1 month. I was totally lost in the jungle of spaghetti code lines. I didn't have any structure at all, even though i fought so badly to implement one.
Then I realized that I have to have structure and code the right way. I started to use CodeIgniter. That really gave me structure and maintainable code. A lot of users say that frameworks are slowing things down, but I think they missed the picture. The code must be maintainable and easy to understand.
Framework + OOP + MVC made my web application so structured so that adding features was not a problem anymore.
When i create a model, I tend to think that it is representing a data object. Maybe a form or even a table/database. So I thought about ORM (doctrine). Maybe this would be yet another great implementation into my web application giving it more structure so I could focus on the features and not repeating myself.
However, I have never used any ORM before and I have only learned the basics of it, why it's good to use and so on.
So now Im asking all of you guys that just like me are striving for maintainable code and know how important that is, is ORM (doctrine) a must have for maintainable code just like framework+mvc+oop?
I want more life experience advices than "raw sql is faster" advices, cause if i would only care about raw performance, i should have dropped framework+mvc+oop in the first place and kept living in a coding nightmare.
It feels like it fits so good into a MVC framework where the models are the tables.
Right now i've got like 150 sql queries in one file doing easy things like getting a entry by id, getting entry by name, getting entry by email, getting entry by X and so on. i thought that ORM could reduce these lines, or else im pretty sure that this will grow to 1000 sql lines in the future. And if i change in one column, i have to change all of them! what a nightmare again just thinking about it. And maybe this could also give me nice models that fits to the MVC pattern.
Is ORM the right way to go for structure and maintainable code?
Ajsie,
My vote is for an ORM. I use NHibernate. It's not perfect and there is a sizable learning curve. But the code is much more maintainable, much more OOP. Its almost impossible to create an application using OOP without an ORM unless you like a lot of duplicate code. It will definitely eliminate probably the vast majority of your SQL code.
And here's the other thing. If you're are going to build an OOP system, you'll end up writing your own O/R Mapper anyway. You'll need to call dynamic SQL or stored procs, get the data as a reader or dataset, convert that to an object, wire up relationships to other objects, turn object modifications into sql inserts/updates, etc. What you write will be slower and more buggy than NHibernate or something that's been in the market for a long while.
Your only other choice really is to build a very data centric, procedural application. Yes it may perform faster in some areas. I agree that performance IS important. But what matters is that its FAST ENOUGH. If you save a few milliseconds here and there doing procedural code, your users will not notice the performance increase. But you 'll notice the crappy code.
The biggest performance bottle-necks in an ORM are in the right way to pre-fetch and lazy-load objects. This gets into the n-query problems with ORMs. However, these are easily solved. You just have to performance tune your object queries and limit the number of calls to the database, tell it when to use joins, etc. NHibernate also supports a rich caching mechanism so you don't hit the database at all at times.
I also disagree with those that say performance is about users and maintenance is about coders. If your code is not easily maintained, it will be buggy and slow to add features. Your users will care about that.
I wont say every application should have an ORM, but I think most will benefit. Also don't be afraid to use native SQL or stored procedures with an ORM every now and then where necessary. If you have to do batch updates to millions of records or write a very complex report (hopefully against a separate, denormalized reporting database) then straight SQL is the way to go. Use ORMs for the OOP, transactional, business logic and C.R.U.D. stuff, and use SQL for the exceptions and edge cases.
I'd recommend reading Jeffrey Palermo's stuff on NHibernate and Onion Architecture. Also, take his agile boot camp or other classes to learn O/R Mapping, NHibernate and OOP. Thats what we use: NHibernate, MVC, TDD, Dependency Injection.
A lot of users say that frameworks are
slowing things down, but I think they
missed the big picture. The code MUST
BE MAINTAINABLE and EASY TO
UNDERSTAND.
A well-structured, highly-maintainable system is worthless if its performance is Teh Suck!
Maintability is something which benefits the coders who construct an application. Raw performance benefits the real people who use the app for their work (or whatever). So, whose concerns ought to be paramount: those who build the system or those who pay for it?
I know it's not as simple as that, because the customer will eventually pay for a poorly structured system - perhaps more bugs, certainly more time to fix them, more time to implement enhancements to the application. As is usually the case, everything is a trade-off.
I've started developing like you, without orm tools.
Then i worked for companies where software development was more industrialized, and they all use some kind of orm mapping tool (with more or less features). The development is far easier, faster, produce more maintainable code, etc.
But i've also seen the drawbacks of these tools : very slow performance. But it was mostly misuses of the tool (hibernate in that case).
Orm tool are very complex tool, so it is easy to misuse them, but if you have experience with them, you should be able to get nearly the same performances as with raw sql. I would have three advices for you :
If performance is not critical, use an orm tool (choose a good one, i am not developing with php, so i can't give you a name)
Be sure for each feature you add, to check the sql that the orm tool produce and send to the database (thanks to a logging facility for example). Think if it is the way you would have written your queries. Most of the inefficiencies of orm tools come from unwanted data that are gathered from the db, unique request split in multiple ones, etc. Slowness rarely comes from the tool in itself
Do not use the tool for everything. Choose wisely when not to use it (you reduce maintainability each time you do raw db access), but sometimes, it isn't just worst trying to make the orm tool do something it was not developed for.
Edit:
Orm tool are most useful with very complex model : many relationships between entities. Which is most of the time encountered in configuration part of the application, or in complex business part of the application.
So it is less useful if you have only few entities, and if there is less chance they get changed (refactored).
The limit between few entities and many is not clear. I would say more that 50 differents Types (sql tables, without join tables) is many, and less than 10 is few.
I don't know what was used to build stackoverflow but it must have been very carefully performance tested before.
If you want to build a web site that will get such a heavy load, and if you don't have experience with that, try to get someone in your team that have already worked on such sites (performance testing with a real set of data and a representative number of concurrent users is not an easy and fast task to implement). Having someone that have experience with it will greatly speed up the process.
Its very important to have a maintainabilty that is high. Ive developed large scaled web application with lowlevel super high preformance. The big disadvantage was maintaining the system, that is, developing new features. If you'r to slow developing the customers will look for other systems/applications.. Its a trade of. Most of the orms has features if you need to do optmized queries direct to sql. The orm itself isnt the bottleneck. Ill say its more about a good db design.
I think you missed the picture. Performance is everyday for your users, they care not at all about maintainability. You are being ethnocentric, you are concerned only for your personal concerns and not those of the the people who pay for the system. It isn't all about your convenience.
Perhaps you should sit down with the users and watch them use your system for day or two. Then you should sit down at a PC that is the same power as the ones they use (not a dev machine) and spend an entire week doing nothing but using your system all day long. Then you might understand their point.

Commitment to Zend Framework - any arguments against? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I am refurbishing a big CMS that I have been working on for quite a number of years now. The product itself is great, but some components, the Database and translation classes for example, need urgent replacing - partly self-made as far back as 2002, grown into a bit of a chaos over time, and might have trouble surviving a security audit.
So, I've been looking closely at a number of frameworks (or, more exactly, component Libraries, as I do not intend to change the basic structure of the CMS) and ended up with liking Zend Framework the best. They offer a solid MVC model but don't force you into it, and they offer a lot of professional components that have obviously received a lot of attention (Did you know there are multiple plurals in Russian, and you can't translate them using a simple ($number == 0) or ($number > 1) switch? I didn't, but Zend_Translate can handle it. Just to illustrate the level of thorougness the library seems to have been built with.)
I am now literally at the point of no return, starting to replace key components of the system by the Zend-made ones. I'm not really having second thoughts - and I am surely not looking to incite a flame war - but before going onward, I would like to step back for a moment and look whether there is anything speaking against tying a big system closely to Zend Framework.
What I like about Zend:
As far as I can see, very high quality code
Extremely well documented, at least regarding introductions to how things work (Haven't had to use detailed API documentation yet)
Backed by a company that has an interest in seeing the framework prosper
Well received in the community, has a considerable user base
Employs coding standards I like
Comes with a full set of unit tests
Feels to me like the right choice to make - or at least, one of the right choices - in terms of modern, professional PHP development.
I have been thinking about encapsulating and abstracting ZF's functionality into own classes to be able to switch frameworks more easily, but have come to the conclusion that this would not be a good idea because:
it would be an unnecessary level of abstraction
it could cost performance
the big advantage of using a framework - the existence of a developer base that is familiar with its components - would partly be cancelled out
therefore, the commitment to ZF would be a deep one. Thus my question:
Is there anything substantial speaking against committing to the Zend Framework?
Do you have insider knowledge of plans of Zend Inc.'s to go evil in 2011, and make it a closed source library? Is Zend Inc. run by vampires run by evil vampires that want to take over the earth? (It was established in the comments that Zend is actually run by vampires.) Are there conceptual flaws in the code base you start to notice when you've transitioned all your projects to it? Is the appearance of quality code an illusion? Does the code look good, but run terribly slow on anything below my quad-core workstation?
Accepting answer
Thanks a lot everyone for your detailed feedback. I wish I could set up a bounty and distribute it evenly among all answerers.
Among many opinions favourable towards ZF, there was one very well founded one against. I took that very seriously and had a close look at alternatives, mainly Yii and Kohana. From that comparison and from reading some more opinions regarding ZF and competing products, I can see that Zend can be viewed as bloated in some fields compared to more minimalistic frameworks. (I can also see that this "bloat" is mostly with good reason to provide maximum flexibility. But the question whether you want maximum flexibility and deal with the ensuing complexity, or a simpler approach with clear guidelines, is a valid one.)
Anyway, I will go for Zend for the project at hand, because the main use I have for the framework there is as a component library. I do not want to adopt Zend's MVC model, I just need high-quality components for Internationalization, Session handling and so on. Because I am building a redistributable product, Zend's flexibility (e.g. the support for five different dictionary formats) is welcome to me. Also, ZF seems to be the only framework that allows the degree of freedom I want (No forced use of patters, file structures...) as far as I can see, no other framework offers that.
For future projects in which I want to make use of the actual MVC features, and totally submit to a framework's conventions on application building, naming, style, and procedures, though, I may not necessarily be going for Zend, but for a more minimalistic framework like Yii or Kohana.
Zend Framework is the best choice. Best framework API of them all, readable code, language conventions, good docs, comunity, support et all
My dislikes of it (subjective, mabe people are going to downvote me for it) are:
Zend_Form, has it's use, but in general is too obtrusive, I just want to structure my HTML not fight APIs and decorators.
Zend_Db_Table, powerful, but it needs a lot of work to accomplish your goals, and Rails taught me to be lazy. No, I don't want to write 3 classes for a model, one for the table, one for the rowset, one for the row, then bind them to each other and so on. I might need the table data gateway at some point, but for now I really want to interface this data with a quick active record.
no active record. With the late static binding in php 5.3 this might change ...
I tried really hard to use these two for a couple of months until I finally had it.
I overcame them by (ideas from Ruby on Rails)
use plain view helpers instead of Zend_Form as in:
echo $this->formText('email', 'you#example.com', array('size' => 32));
having my own Active Record like models ( http://www.phpactiverecord.org )
validate and filter on the model
for the really extreme corner cases, one can fall back to Zend_Form + Zend_Db_Table, although I never felt the need.
EDIT
There are some new kids worth checking out, like Laravel
The one thing ZF really wins over other frameworks is the router, controller and views, conventions, clean readable code, process.
Unless you're looking forward for a massive, giant project with 20+ developers, I'd - if I were you - would do anything including sacrificing an arm and/or a leg just to avoid Zend Framework.
The nice option you found out might look handy - to the point where you find other 1k+ setting that just looks like a waste of time and developer effort in the library. You'll soon find yourself in the middle of Customization Ocean, overwhelmed by settings, interfaces and abstract classes.
The documentation is not only detailed, but as a result very lengthy and complicated (similarly to the library itself). I want 3rd part classes to help me and not to get into my way.
The most important factor was the development speed for me, obviously. Without a bunch of colleges around you, Zend Framework should be seriously considered whether to be dropped.
There are too many people making wishes in the Zend issue tracker, too many people implementing code. To date, I've yet to see any serious vision behind those millions of lines.
My two cents really comes to down to one point: despite (or rather, because of?) it is backed by the PHP company, it is far too bloated for personal usage and small- and medium-sized projects.
My team is currently using Yii for a medium-sized project. It's not perfect either, but way more usable and developer-friendly compared to the big brother.
I don't have any big argument against ZF -- on the contrary ; of course, I have not used all the components yet (not sure anybody has), but I've never seen anything that looked "bad" when going through the sources.
When beginning a new project, I would generally go with Zend Framework myself, actually, if I'm the one who has to choose...
There's at least one argument I would like to add to your list :
Zend Framework can integrate with other components : you talked of using components of ZF in your application, but didn't talk about the opposite.
A great example is Doctrine (the default ORM of Symfony), which can be used with ZF quite easily -- and is much better than Zend_Db, in my opinion !
Besides that, I have to say that I agree with everyone of your points.
About the encapsulating ZF classes into your own classes : yes, you could do that (I sometimes do), but I wouldn't recommend doing it for everything : in most cases, it will probably not be necessary.
About the future :
Zend Framework is released under a BSD licence -- which means what exists cannot be closed-sourced.
Closing it would mean its end, anyway -- would be a stupid move, in the context of the PHP community
Work on ZF 2.0 is just starting (With PHP 5.3 as a requirement, btw)
Maybe, depending on your schedule for your application, it could be interesting ?
At least if you don't need to start developping before at least a couple of month...
We've been developing with ZF for almost a year now, and I really have no major qualms with it. Had a bit of a learning curve, but once I understood it, I realized how well put together this framework really is.
Compared to other frameworks, some people might point out the lack of a Model library as being a downside. I actually prefer not being told what to use, and integrating Doctrine with ZF is frictionless. If you're developing in PHP 5.3, and want an ORM, I would highly recommend Doctrine 2 (caveat: it's still in alpha testing).
The other great thing is being able to pick and choose what you want or don't want. I'm not a huge fan of Zend_Form, but I don't have to use it if I don't want to. Also, there's a very loosely structured plugin architecture that lets you really easily hook your own libraries into the framework.
So, I'm a pretty big fan of it.
One thing I am missing in the Zend Framework is a proper OR mapper. Zend_DB is ok but has some shortcomings, especially when dealing with huge databases (many tables) it gets very cumbersome. But there are a couple of OR mappers out there that can be integrated (e.g. zend-framework-orm or Doctrine as mentioned by Pascal MARTIN).
But yes, Zend is excellent, very powerful and I feel it is in some ways what PHP actually should be/should have been in terms of interface and functionality.
What I especially like apart from the obvious is the support for Dojo for rich client applications and the SOAP support.
I used ZF on a project as sole developer. Although the learning curve was quite steep, I didn't encounter too many of the problems described above. In general I found the framework very flexible, and the loose-coupling of the components made it easy to do my own thing when the ZF way of doing it didn't suit my needs.
But having recently started using python, I'd say use python ;)

PHP: A Personal Framework

I'm going to write a framework for my web projects in PHP.
Please don't tell me about considering to use some existing framework (Cake, CodeIgniter, Symfony, etc.) - I have already had a look at them and decided to write one for myself.
The framework itself will mainly consist of a module system, a database handler and a template parser. (Many other things too, of course)
With module system I mean that every module has exactly one PHP file and one or more templates associated with it.
An example module would be modules/login.php that uses templates/login.tpl for its design.
These days everyone(?) is talking about the MVC (Model View Controller) concept and most of the existing frameworks use it, too.
So my questions are the following:
Is MVC really effective for a personal framework?
Would it be a bad idea to use a module system?
Did you ever write a framework for yourself? What are your experiences?
Is MVC really effective for a personal framework?
Yes, it can be. Although, it might be a little overkill (which, is not necessarily a bad thing if you are trying to learn)
Would it be a bad idea to use a module system?
This is never a bad idea.
Did you ever write a framework for yourself? What are your experiences?
I wrote a common security framework for my group's PHP applications when I was an intern. I learned alot, but the project as a whole might have benefited more from a pre-built solution.
Of course, I wouldn't have learned as much if I just installed a pre-built solution. So you always have to take that into account, especially for personal projects. Sometimes re-inventing the wheel is the only way you will learn something well.
Is MVC really effective for a personal framework?
What MVC means anymore, due to its vague interpretation, is business logic, presentation, and input handling. So, unless you aim to design an application that does not involve any three of those, MVC is, in its vague sense, very suitable.
Often it can be more formal than you desire, however, as it demands physical separation of ideas into different code files. Quick and dirty tasks or rapid prototyping might be more quickly setup if the formalities are avoided.
In the long term, what MVC asks for is beneficial to the sustainability of the application in ways of maintenance and modification or addition. You will not want to miss this. Not all frameworks encourage the right practices, though. I am not surprised that you find the various implementations you've tried insufficient. My personal favourite is Agavi. To me and others, in a world of PHP frameworks that do not feel right, Agavi emerges to do the right things. Agavi is worth the shot.
Would it be a bad idea to use a module system?
MVC asks you to separate components of business logic, presentation, and input handling, but it does not suggest how to layout the files. I presume this is the challenge you are addressing with a module system. To answer your question: modules serve identically to sub-directories. If the items are few, its probably more hassle to bother with subdirectories even if the files could logically be separated into them. When the number of items grow large, its now cumbersome to locate them all and sub-directories become a better option.
Frameworks will tack on functionality that allows you to deal with modules as their own configurable entity. The same functionality could just as well exist without modules, perhaps in a more cumbersome manor. Nonetheless, do not consider modules primarily as a system. Systems are so wonderfully vague that you can adapt them to whatever setup you find suitable.
Did you ever write a framework for yourself? What are your experiences?
Yes I have wrote several frameworks with various approaches to solving the issues of web applications. Every such framework I wrote became nothing but a vital learning curve. In each framework I made I discovered more and more the issues with building software. After failing to create anything interesting, I still gained because when asked to make a program I could fully do so with justice.
I recommend you continue if this is the sort of learning experience you want. Otherwise, give Agavi a shot. If that too fails, ensure that you have a clear and detailed specification of what your framework will do. The easiest way to barge into making software, work really hard, and accomplish nothing is to not decide before-hand what exactly your software will do. Every time I ran into making code the only thing in my mind was I will do it right. What happened was a different story: oh, well I need to make a routing system as that seems logical; hmm, okay, now I need a good templating system; alright, now time for the database abstraction; but gee, what a lot of thinking; I should look to the same system from software XXY for inspiration. Therein is the common cry that pleads to use existing software first.
The reason I thought I could do it right was not because all the nuts and bolts of the framework felt wrong. In fact, I knew nothing about how right or wrong they were because I never worked with them. What I did work with was the enamel, and it felt wonky. The quickest way to derive your own framework is really to steal the nuts and bolts from another and design your own enamel. That is what you see when building an application and frankly is the only part that matters. Everything else is a waste of your time in boilerplate. For learning how to build software, however, its not a waste of time.
If you have any other questions, please ask. I am happy to answer with my own experience.
I am also actually writing a php framework with a friend of mine. I absolutely can understand what you do.
I thing what you are doing is near mvc. You have the templates as views. And the modules as controller. So I think that is ok. The only thing you need is the model. That would be some kind of active records.
In my framework there are simular concepts, except we are writing our own active records engine at the moment. I think what you do isn't bad. But it's hard to say without seeing code.
I see only one problem you have to solve. A framework should be perfectly integrated. It is always a complicated to make your module look nice integrated without always have to think of module while you are coding application.
Is MVC really effective for a personal framework?
Would it be a bad idea to use a module system?
Yes it is. But MVC is such a loosy-goosy design pattern that you can draw the line between model, view, and controller anywhere you want. To me, the most important parts are the model and the view. I simply have pages, php modules, that generate html by filling in a template from a database. The pages are the view and the database is the model. Any common application-specific code can be factored out into "controllers". An example might be a common, sophisticated query that multiple pages must use to render data.
Other than that I have utilities for safe database access, simple templating, and other stuff.
Did you ever write a framework for yourself? What are your experiences?
Yes. I'm very glad I did. I can keep it simple. I know intimately how it works. I'm not dependent on anyone but myself. I can keep it simple yet useful.
Some pointers (0x912abe25...):
Every abstraction comes with a cost.
Don't get to fancy. You might regret not keeping it simple. Add just the right amount of abstraction. You may find you over-abstracted and something that should be simple became excessively complex. I know I've made this mistake. Remember You-aint-gonna-need-it.
Scope your variables well
Don't load your pages by doing
include_once('...page file ...');
where it's expected that page file will have a bunch of inline php to execute looking up different global variables. You lose all sense of scope. This can get nasty if you load your page file from inside a function:
function processCredentials()
{
if (credentialsFail)
{
include_once('loginpage.php');
}
}
Additionally, when it comes to scoping, treat anything plugged into templates as variables with scope. Be careful if you fill in templates from something outside the page file associated with that template (like a master index.php or something). When you do this it's not clear exactly what's filled in for you and what you are required to plug into the template.
Don't over-model your database with OO.
For simple access to the database, create useful abstractions. This could be something as simple as fetching a row into an object by a primary index.
For more complex queries, don't shy away from SQL. Use simple abstractions to guarantee sanitization and validation of your inputs. Don't get too crazy with abstracting away the database. KISS.
I would say that MVC makes more sense to me, since it feels better, but the only practical difference is that your login.php would contain both the model (data structure definitions) and the controller (code for page actions). You could add one file to the module, e.g. class.login.php and use __autoload() for that, which would essentially implement an MVC structure.
I have refactored a big PHP project to make it more MVC compliant.
I found especially usefull to create a DAO layer to centralize all database accesses. I created a daoFactory function, which creates the DAO and injects the database handle into it (also the logger, I used log4php, got injected).
For the DAO, i used a lot the functionalities of the database (mysql), like stored procedure and triggers. I completly agree with Doug T. about avoid over-abstraction, especially for database access : if you use the DB properly (prepared statements, etc.) you don't need any ORM and your code will be much faster. But of course you need to learn mysql (or postgress) and you become dependant on it (especially if you use a lot of stored procedure, like I tend to do).
I am currently refactoring a step further, using the Slim php framework and moving toward a restfull api : in this case there is no view anymore because everything is outputted as json. But I still use smarty because its caching works well and I know it.
Writing a framework could be a rewarding experience. The important thing to consider is that you do not write a framework for its own sake. The reason one writes a framework is to make development easy.
Since it is a personal framework you should think in terms of how it could help you develop with less hassle.
I do not think a template system is a good idea. Think of it - what is the major benefit of using a template system? The answer is that it helps teams with different skill sets jointly develop an application. In other words, some members of the team can work on the user interface and they do not need to be PHP coders. Now, a personal framework will most likely be used by a single person and the benefit of template system becomes irrelevant.
All in all, you should look at your own coding habits and methods and discover tasks that take most of your time on a typical project. Then you should ask yourself how you can automate those tasks to require less time and effort. By implementing those automation mechanisms you will have to stick to some sort of conventions (similar to an API). The sum of the helper mechanisms and the conventions will be your personal framework.
Good luck.
MVC doesn't work
you don't want to be constrained in the structure of your "modules"; also, keep templates close to the code (the templates directory is a bad idea)
no
re 1.: see Allen Holub's Holub on Patterns. briefly: MVC basically requires you to give up object oriented principles.
Tell Don't Ask is a catchy name for a mental trick that helps you keep the data and code that acts on it together. Views cause the Model to degrade into a heap of getters and setters, with few if any meaningful operations defined on them. Code that naturally belongs in the Model is then in practice spread among Controllers and Views(!), producing the unhealthy Distant Action and tight coupling.
Model objects should display themselves, possibly using some form of Dependency Injection:
interface Display
{
function display($t, array $args);
}
class SomePartOfModel
...
{
function output(Display $d)
{
$d->display('specific.tpl', array(
'foo' => $this->whatever,
...
));
}
}
OTOH, in practice I find most web applications call for a different architectural pattern, where the Model is replaced with Services. An active database, normalized schema and application specific views go a long way: you keep the data and code that acts on it together, and the declarative nature makes it much shorter than what you could do in PHP.
Ok, so SQL is a terribly verbose language. What prevents you from generating it from some concise DSL? Mind you, I don't necessarily suggest using an ORM. In fact, quite the opposite. Without Model, there's little use for an ORM anyway. You might want to use something to build queries, though those should be very simple, perhaps to the point of obviating such a tool...
First, keep the interface your database exposes to the application as comfortable for the application as possible. For example, hide complex queries behind views. Expose update-specific interfaces where required.
Most web applications are not only the owners of their respective underlying databases, they're their only consumers. Despite this fact, most web applications access their data through awkward interfaces: either a normalized schema, bare-bones, or a denormalized schema that turned out to make one operation easier at the price of severe discomfort elsewhere (various csv-style columns etc). That's a bit sad, and needlessly so.
re 2.: it's certainly good to have a unified structure. what you don't want to do is to lock yourself into a situation where a module cannot use more than one file.
templates should be kept close to code that uses them for the same reason that code that works together should be kept together. templates are a form of code, the V in MVC. you'll want fine-grained templates to allow (re)use. there's no reason the presentation layer shouldn't be as DRY as other parts of code.

Categories