ID:               46750
 Comment by:       rarioj at gmail dot com
 Reported By:      ms419 at freezone dot co dot uk
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: Debian
 PHP Version:      5.2.6
 New Comment:

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().


Previous Comments:
------------------------------------------------------------------------

[2008-12-04 16:51:09] 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% 


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=46750&edit=1

Reply via email to