#37450 [Bgs->WFx]: Type Hinting with Array of Objects
| From: | derick@php.net | Date: | Mon, 15 May 2006 21:55:22 +0000 |
| Subject: | #37450 [Bgs->WFx]: Type Hinting with Array of Objects | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-97144@lists.php.net to get a copy of this message | ||
ID: 37450
Updated by: derick@php.net
Reported By: mega-squall at caramail dot com
-Status: Bogus
+Status: Wont fix
Bug Type: Feature/Change Request
Operating System: ALL
PHP Version: 5.1.4
New Comment:
Its a valid request... so marking as "Wont fix" instead.
Previous Comments:
------------------------------------------------------------------------
[2006-05-15 21:53:38] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
That's impossible and never will be.
But you can do that more or less yourself.
Derive a class that implements ArrayAccess or reuse
ArrayObject/ArrayIterator. And overload its offsetSet(). In the method
body you add the check to ensure only what you want can go in.
Now if that class accepts only B's you may call it ArrayOfB and the use
ArrayOfB as you typehint.
So obviously this is a question for general@php.net
------------------------------------------------------------------------
[2006-05-15 21:47:29] mega-squall at caramail dot com
Description:
------------
Type Hinting is compatible with array and objects, but not with array
containing ONLY instances of single chosen class (array of instances).
This would be appreciated, if relevant with the structure of arrays.
Reproduce code:
---------------
class B {
public $field;
public __construct ($param) { $this->field = $param; }
}
class A {
public function method (B[] $parameters){
foreach ($parameters as $object) {
echo $object->field . "\n";
}
}
}
$b = Array ();
for ($count=0; $count<10; $count++) {
$b[] = new B ($count);
}
$a = new A ();
$a->method ($b);
Expected result:
----------------
0
1
2
3
4
5
6
7
8
9
Actual result:
--------------
Parse error: syntax error, unexpected '[', expecting '&' or T_VARIABLE
in __FILE__ on line __LINE__
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=37450&edit=1