|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-10-23 18:50 UTC] phpbugs at sevenlight dot com
Description:
------------
Using a type-hint with a namespace does not work. It allows an object of a different class to be passed, and even allows a bogus namespace to be used. It will also allow null variables, and will pass through any value, no matter what class type-hinting you use.
Unfortunately, in my simplest test cases I have not been able to reproduce it, it is only in my larger project that it causes this problem.
I really don't know where to start with this, and how to duplicate it. Needless to say, have provided the failing code from my project. The entire project is several thousand lines of code. And I've really done my best to track down the problem, even using the 5.3-alpha3-dev.
I'll include the code that causes the problem.
I really don't know what's wrong, and why it is doing it. Any hints as to what I might be able to do to try to reproduce this in a simpler way would be nice, but I really don't know where to go from here.
Reproduce code:
---------------
<?php
namespace MyNamespace;
class MyClass
{
// execute
public function execute()
{
$this->test(NULL);
return $this->_execute($this->_mc);
}
protected function test(MyBogusNamespace::MyBogusClass $m)
{
$reflect = new ReflectionClass(get_class($this));
$method = $reflect->getMethod('test');
echo $method;
var_export($m);
echo "\n\nClass: " . get_class($m) . " == MyBogusNamespace::MyBogusClass\n\n";
}
}
?>
Expected result:
----------------
Catchable fatal error: Argument 1 passed to MyNamespace::MyClass::test() must be an instance of MyBogusNamespace::MyBogusClass, null given, called in /srv/www/test/ns/new.php on line 9 and defined in /srv/www/test/ns/new.php on line 13
Actual result:
--------------
Method [ <user> protected method test ] {
@@ /srv/spin/spin4/spinnaker/extender/universal/tablelibrary.php 73 - 80
- Parameters [1] {
Parameter #0 [ <required> MyBogusNamespace::MyBogusClass $m ]
}
}
NULL
== MyBogusNamespace::MyBogusClass
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 09:00:01 2025 UTC |
Using $x = new myclass; $x->execute();, i got: Catchable fatal error: Argument 1 passed to MyNamespace::MyClass::test() must be an instance of MyBogusNamespace::MyBogusClass, null given, called ... If you want accept NULL to be passed, it must be the default value to the parameter. public function test(MyBogusNamespace::MyBogusClass $m = NULL) So, we will have... Method [ <user> public method test ] { @@ ... 15 - 22 - Parameters [1] { Parameter #0 [ <optional> MyBogusNamespace::MyBogusClass or NULL $m = NULL ] } } Thanks.Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. Well, I can't reproduce it. namespace MyNamespace; class foo { public function test(bla::foobar $a) { } } $x = new foo; #$x->test(new stdclass); /* Catchable fatal error: Argument 1 passed to MyNamespace::foo::test() must be an instance of bla::foobar, instance of stdClass given ... */ #$x->test(NULL); /* Catchable fatal error: Argument 1 passed to MyNamespace::foo::test() must be an instance of bla::foobar, null given, called in ... */ $reflect = new ReflectionClass(__NAMESPACE__ .'::foo'); $y = $reflect->getMethod('test'); #$y->invoke(new foo, 1); /* Catchable fatal error: Argument 1 passed to MyNamespace::foo::test() must be an instance of bla::foobar, integer given */