Req #46750 [Com]: allow additional, optional arguements to __get()

From: Date: Mon, 10 Nov 2014 19:09:35 +0000
Subject: Req #46750 [Com]: allow additional, optional arguements to __get()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188542@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=46750&edit=1

 ID:                 46750
 Comment by:         cmbecker69 at gmx dot de
 Reported by:        ms419 at freezone dot co dot uk
 Summary:            allow additional, optional arguements to __get()
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 Operating System:   Debian
 PHP Version:        5.2.6
 Block user comment: N
 Private report:     N

 New Comment:

Optional arguments to __get() make no sense, because manually
calling __get() is flawed, to start with. Use a dedicated accessor
method instead.


Previous Comments:
------------------------------------------------------------------------
[2009-03-19 04:50:06] rarioj at gmail dot com

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

------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=46750&edit=1


Thread (4 messages)

« previous php.bugs (#188542) next »