php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #46750 allow additional, optional arguements to __get()
Submitted: 2008-12-04 16:51 UTC Modified: 2017-05-08 12:19 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: ms419 at freezone dot co dot uk Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.6 OS: Debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ms419 at freezone dot co dot uk
New email:
PHP Version: OS:

 

 [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% 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-19 04:50 UTC] 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().
 [2014-11-10 19:09 UTC] cmbecker69 at gmx dot de
Optional arguments to __get() make no sense, because manually
calling __get() is flawed, to start with. Use a dedicated accessor
method instead.
 [2017-05-08 12:19 UTC] peehaa@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2017-05-08 12:19 UTC] peehaa@php.net
Closing this bug as wont-fix.

It's weird  to manually call the magic __get method.

In all its time there hasn't been much support for this bug anyway.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 21:01:28 2024 UTC