An open API service indexing awesome lists of open source software.

https://github.com/chadicus/extension_methods

Extension methods enable you to "add" methods to existing classes without creating a new derived class or otherwise modifying the original class.
https://github.com/chadicus/extension_methods

Last synced: 4 months ago
JSON representation

Extension methods enable you to "add" methods to existing classes without creating a new derived class or otherwise modifying the original class.

Awesome Lists containing this project

README

          

# extension_methods
Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended class. For client code written in PHP, there is no apparent difference between calling an extension method and the methods that are actually defined in a class.

## Theory of Operation

Class is written that offers additional functionality to a class

```php
query($xpath);
if ($list === false) {
throw new \DOMException("XPath {$xpath} is not valid.");
}

if ($list->length) {
$list->item(0)->nodeValue = $value;
return;
}

$pointer = $document;
foreach (array_filter(explode('/', $xpath)) as $tagName) {
$pointer = self::parseFragment($domXPath, $pointer, $tagName);
}

$pointer->nodeValue = $value;
}

// other methods...
}
```

Extension class is registered.

```php
register_class_extensions('\DOMDocument', 'DOMDocumentExtensions');
```

New methods can be used on instanciated object

```php
$dom = new \DOMDocument();
$dom->addXPath('/path/to/new/node', 'the node value');
```