phpQuery::extend PHP Method

extend() public static method

Extend class namespace.
public static extend ( string | array $target, array $source ) : unknown_type
$target string | array
$source array
return unknown_type
    public static function extend($target, $source)
    {
        switch ($target) {
            case 'phpQueryObject':
                $targetRef =& self::$extendMethods;
                $targetRef2 =& self::$pluginsMethods;
                break;
            case 'phpQuery':
                $targetRef =& self::$extendStaticMethods;
                $targetRef2 =& self::$pluginsStaticMethods;
                break;
            default:
                throw new Exception('Unsupported $target type');
        }
        if (is_string($source)) {
            $source = array($source => $source);
        }
        foreach ($source as $method => $callback) {
            if (isset($targetRef[$method])) {
                //				throw new Exception
                self::debug("Duplicate method '{$method}', can\\'t extend '{$target}'");
                continue;
            }
            if (isset($targetRef2[$method])) {
                //				throw new Exception
                self::debug("Duplicate method '{$method}' from plugin '{$targetRef2[$method]}'," . " can\\'t extend '{$target}'");
                continue;
            }
            $targetRef[$method] = $callback;
        }
        return true;
    }

Usage Example

Example #1
0
 public function testExtend()
 {
     $newMethods = array('newMethod1' => array($this, 'callback1'), 'newMethod2' => array($this, 'callback2'));
     phpQuery::extend('phpQueryObject', $newMethods);
     $doc = phpQuery::newDocumentXML("<div/>");
     $this->assertTrue($doc->newMethod1() == $doc, '$doc->newMethod1 == $doc');
     $this->assertTrue($doc->newMethod2() == "callback2", '$doc->newMethod1 == "callback2"');
 }
All Usage Examples Of phpQuery::extend