Skip to content
tobiasz.cudnik edited this page Oct 24, 2008 · 18 revisions
  1. labels Featured,Phase-Implementation
Although *phpQuery* is a jQuery port, there is extensive PHP-specific support.

Table of Contents

Table of Contents

 * [#Class_Interfaces Class Interfaces]
   * [#Iterator Iterator Interface]
   * [#Array_Access ArrayAccess]
   * [#Countable Countable Interface]
 * [http://code.google.com/p/phpquery/wiki/Callbacks Callbacks]
 * [#PHP_Code_Support PHP Code Support]
   * [#Opening_PHP_files_as_DOM Opening PHP files as DOM]
   * [#Inputting_PHP_code Inputting PHP code]
   * [#Outputting_PHP_code Outputting PHP code]

Class Interfaces

phpQuery implements some of Standard PHP Library (SPL) interfaces.

Iterator

Iterator interface allows looping objects thou native PHP *foreach loop*. Example:

Now there is a catch above. Foreach loop *doesn't return phpQuery object*. Instead it returns pure DOMNode. That's how jQuery does, because not always you need *phpQuery* when you found interesting nodes.

Array Access

If you like writing arrays, with phpQuery you can still do it, thanks to the ArrayAccess interface.

Countable

If used to do `count($something)` you can still do this that way, instead of eg `pq('p')->size()`.

Callbacks

There is a special Callbacks wiki section, to which you should refer to.

PHP Code Support

Opening PHP files as DOM

PHP files can be opened using *phpQuery::newDocumentPHP($markup)* or *phpQuery::newDocumentFilePHP($file)*. Such files are visible as DOM, where:

 * PHP tags beetween DOM elements are available (queryable) as `<php> ...code... </php>`
 * PHP tags inside attributes are HTML entities
 * PHP tags between DOM element's attributes are *not yet supported*

Inputting PHP code

Additional methods allows placing PHP code inside DOM. Below each method visible is it's logic equivalent.

 * *attrPHP*($attr, $code)
   * [http://docs.jquery.com/Attributes/attr attr]($attr, "<?php $code ?>")
 * *addClassPHP*($code)
   * [http://docs.jquery.com/Attributes/addClass addClass]("<?php $code ?>")
 * *beforePHP*($code)
   * [http://docs.jquery.com/Manipulation/before before]("<?php $code ?>")
 * *afterPHP*($code)
   * [http://docs.jquery.com/Manipulation/after after]("<?php $code ?>")
 * *prependPHP*($code)
   * [http://docs.jquery.com/Manipulation/prepend prepend]("<?php $code ?>")
 * *appendPHP*($code)
   * [http://docs.jquery.com/Manipulation/append append]("<?php $code ?>")
 * *php*($code)
   * [http://docs.jquery.com/Manipulation/html html]("<?php $code ?>")
 * *wrapAllPHP*($codeBefore, $codeAfter)
   * [http://docs.jquery.com/Manipulation/wrapAll wrapAll]("<?php $codeBefore?><?php $codeAfter ?>")
 * *wrapPHP*($codeBefore, $codeAfter)
   * [http://docs.jquery.com/Manipulation/wrap wrap]("<?php $codeBefore?><?php $codeAfter ?>")
 * *wrapInnerPHP*($codeBefore, $codeAfter)
   * [http://docs.jquery.com/Manipulation/wrapInner wrapInner]("<?php $codeBefore?><?php $codeAfter ?>")
 * *replaceWithPHP*($code)
   * [http://docs.jquery.com/Manipulation/replaceWith replaceWith]("<?php $code ?>")

Outputting PHP code

Code inserted with methods above won't be returned as valid (runnable) using classic output methods such as *html()*. To make it work, *php()* method without parameter have to be used. Optionaly *phpQuery::markupToPHP($markup)* can activate tags in string outputed before.

  • REMEMBER* Outputing runnable code and placing it on webserver is always dangerous !
Clone this wiki locally