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.
- Host: GitHub
- URL: https://github.com/chadicus/extension_methods
- Owner: chadicus
- Created: 2016-05-05T20:38:54.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-05-05T20:51:35.000Z (about 10 years ago)
- Last Synced: 2025-02-25T00:49:33.009Z (over 1 year ago)
- Language: M4
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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');
```