|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-12-04 16:51 UTC] ms419 at freezone dot co dot uk
Description:
------------
I wish it were possible to declare a __get() magic method with additional, optional arguments, in addition to the required $name argument.
It makes total sense for any __get() magic method declaration to *require* exactly one argument, but it would be nice to allow additional, optional arguments. Optional arguments would get default values when __get() is called magically, but could be specified if __get() is called manually.
This is already the case with the ArrayAccess interface (it makes sense that there is a difference between SPL interfaces and PHP magic methods, but optional arguments is a pattern I find useful with ArrayAccess and would find useful with __get()...)
I successfully declare offsetGet() with additional, optional arguments:
<?php
class Foo implements ArrayAccess
{
public function offsetGet($offset, array $options = array())
{
[...]
}
[...]
}
Reproduce code:
---------------
<?php
class Foo
{
public function __get($name, array $options = array())
{
}
}
Actual result:
--------------
ket% php foo.php
Fatal error: Method Foo::__get() must take exactly 1 argument in /home/jablko/foo.php on line 7
ket%
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 13:00:01 2025 UTC |
The function func_get_args() can be used to capture all passed arguments to the called method or function, regardless of the minimum number of parameters required. So for example: function __get($key) { if (!isset($this->container[$key])) { return func_get_arg(1); // default to the second parameter index } return $this->container[$key]; } somewhere within the class: $this->__get('name', 'default value'); // the second argument will be the second parameter to the magic method __get().