Write PHP like JQuery
Posted: May 6, 2010
So I’ve certainly made my opinion of JQuery clear. As far as I’m concerned it’s really an integral part of the JavaScript language. The main thing it brings to the table is its drop-dead simple css-style selection and manipulation of the DOM.
However, another thing that many JQuery programmers really appreciate is the command chaining lets you pipe the object from one statement to another, futher shrinking the number of lines of code and in the process making it easy to just do a lot of different things in a row to one object.
$('.selectableClass').removeClass('selectableClass')
.addClass('unselectableClass').parent().children().slideUp();
That code is really concise, but it’s still pretty self-explanatory what’s going on. It doesn’t reach the level of unreasonable terseness of, say, the frackin’ ternary operator. (die ternary die!).
Wouldn’t it be great to make some of your PHP objects act like that? Guess what? You can!
There are a number of design patterns that can encourage working in your code this way. Decorators, for example. Obviously the factory pattern is all about creating and using brand new objects.
All you have to do is just pass the object you’re wanting to chain along back as the result of a method call. For example, you might have some kind of collection object that you add a new item to and then want to use that item immediately, as in the following example:
public function addItem($childItem) {
// add the child item
return $childItem
}
That’s all there is to it. You can then call the function like:
$collection->addItem(new childItem())->doSomethingOnChild();
In a more concrete example, say you’re creating a spreadsheet. You may want to create a new worksheet and new row all from a database.
$spreadsheet = new Spreadsheet()->addWorksheet(new Worksheet())->addRow(new Row())->manipulateRow();
Here, we’re piping in new objects as we create them and then using them right away.
Obviously this can get to be too much, so don’t go overboard with it. The above is getting a little bit long. But it can be a great practice to learn for certain design patterns and for hierarchial object relationships. And it shows that there’s more than one way to code your objects. It’s all about options.

I work for myself new teen threesome porn %-OO
Where do you study? naomi moira model 2464
Photography Stunning Babe Models
jyvddo
I’m a housewife trini models nude vmf
I’d like to pay this cheque in, please Nymphet Lolitas zdq
What’s the last date I can post this to to arrive in time for Christmas? Lolita Tgp
%[[
US dollars Vladmodel Alina Topless
cgvfvr
Which team do you support? Supermodels Big Tits
qjaoq
I’ve got a part-time job Young Models Nn
47993
VIP такси Mercedes на заказ для ценителей роскоши и блеска.
Ресурс для тех, кто желает создавать собственную музыку
Беттинг – ставки на курсы валют, простая альтернатива форекс
Люминофорная краска – безопасное свечение в 20 раз мощнее фосфора
Интернет-магазин представляет огромный ассортимент плитки
i’m fine good work http://oaqyqoybame.de.tl sun bbs toplist if any chick wants to do my me like that and get me in the porn game then hit my page up!
I’ll call back later http://yyryrikerim.de.tl teen age modells Had to turn the sound down but the chick is fantastic. Wish she’d have pulled a gun out and put the guy out of his misery.
Самые разнообразные очки в столице
I’ve just started at http://rypeqatefuqu.de.tl preteen children fuck Sexy piece does great job with big prick working her tongue up down and around his shaft .. slurping enthusiastically as he unloads his dangerous weapon all over her slutty face
I like watching football http://tofaoqobaj.de.tl animal cell model Damn i love it when she moans and smile when she is getting her tight pussy fucked.How I would love to get some action with her!!!
click to read…
[...]the time to read or visit the content or sites we have linked to below the[...]…
Основная задача в Международной Академии Люблю Любовь