phpQuery - a jQuery port to PHP

Introduction

phpQuery is PHP-port of jQuery - well known and great web2.0 JS library

It's something defferent than jQPie, which is form of JS code generator and server-client layer.

For example You can do something like this:

print _('file.htm')
    ->find('body div.cls1.cls2 ul > li:first')
        ->parent()
            ->prepend('<li>my new first LI</li>')
            ->parents('.myClass')
                ->remove()
                ->end()
            ->appendTo('body')
            ->parents('html')
                ->html();

Code above will find first LI inside specific UL, then move pointer into it's parent (UL), then prepend (add at the begining) new LI, then pointer will move to parent element with class .myClass, which will be removed, and pointer will go back to UL (with end() method), and then UL will be appended to BODY (moved, not copied). Atfer all this operations parent with tag HTML will be searched and it's content will be returned to print statement.

phpQuery acts almost like jQuery - it returns new instance on certain methods and allows to revert stack.

It works on DOM Extension and is designed for PHP5 only.

There is almost no docs yet, so please refer to jQuery's one (DOM section).

Difference against jQuery

phpQuery differs in some cases from jQuery:

  1. Interation
  2. Callbacks
  3. No DOM nodes
  4. In some method names (PHP reserved words)
  5. PHP specific addons
  6. Other addons

Interation

phpQuery makes use of PHP's SPL Interator interafce, so You can do:

foreach(_('ul>*') as $_li) {
	$_li->prepend('new beginning of every LI');
}

Callbacks

PHP doesn't have closures, but You can still use callbacks - direct or created with create_function() like so:

function imTheCallback($_node){
	$_node->html("i'm changed content");
}
class imTheClass {
	static function imTheStaticCallback($_node){
		$_node->html("i'm changed content v2");
	}
	function imTheCallbackToo($_node){
		$_node->html("i'm changed content v3");
	}
}
$class = new imTheClass;
_('ul>*')
	.each('imTheCallback')
	.each(array('imTheClass', 'imTheStaticCallback'))
	.each(array($class, 'imTheCallbackToo'))
	.each(create_function('$_node', '
		$_node->html("i\'m changed content v4");'
	));
}

No DOM nodes

Every node passed to callback or inside iteration is phpQuery object, not a DOM node. Also there isn't a get() method.

Method names

There are several methods in jQuery's interface with names which couldn't be used as PHP class method or was changed to preserve consistent naming convention.

All those methods have been prefixed with _underscore and here's the list:

  • _clone
  • _next
  • _prev
  • _empty

PHP specific addons

There are couple of PHP specific addons in phpQuery for easier developement:

  • appendPHP($code) - equals to append(<?php $code ?>)
  • prependPHP($code) - equals to prepend(<?php $code ?>)
  • beforePHP($code) - equals to before(<?php $code ?>)
  • afterPHP($code) - equals to after(<?php $code ?>)
  • attrPHP($attr, $code) - equals to attr($attr, <?php $code ?>)
  • php($code) - equals to html(<?php $code ?>)
  • phpPrint($code) - equals to html(<?php print $code ?>)
  • phpMeta($selector, $code) - equals to find($selector)->php($code)->end()
  • __toString() - equals to htmlWithTag()

Other addons

There is/will be several methods not present in standard jQuery, which i use (with jQuery) in my projects. More about this later.

Developement status

Actually phpQuery seems to be quite stable and is main part of plainTemplates lib, which powers this blog.

Although there are couple of things to be done:

  • Dedicated docs (copy jQuery's one, add PHP specific, generate phpdoc)
  • Missing methods (css, val)

Download and links

Here are the link which could be helpfull when dealing with phpQuery:

Comments: 0

Tags: PHP jQuery project DOM phpQuery release phpQuery release

Bookmark

  • del.icio.us