Bug #52396 [NEW]: Acessing a class within a static class
| From: | mark at rwrightson dot f9 dot co dot uk | Date: | Wed, 21 Jul 2010 22:42:39 +0000 |
| Subject: | Bug #52396 [NEW]: Acessing a class within a static class | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-152591@lists.php.net to get a copy of this message | ||
From:
Operating system: Windows 7
PHP version: 5.3.2
Package: Class/Object related
Bug Type: Bug
Bug description:Acessing a class within a static class
Description:
------------
It is currently not possible to access a class that is instantiated within
another class that is instantiated within a static object variable.
Using the example below I would expect this line of code to function
correctly:
(where self is Foo)
self::$bar->test->hello();
however this isn't the case. A getter must be created in the bar() class
to get the test class object variable. I.e. getTest(). The above line can
then be re-written as :
self::$bar->getTest()->hello();
this works correctly. In finding this solution I also tried the same
concept in another OO language (Java). I copied the code as per below and
altered the syntax. the line above became: bar.test.hello(); which worked
ok.
For this reason, as the operation discussed here is a standard OO concept,
it could be classed as a bug within PHP.
The attached test script demonstrates the getTest() workaround. commented
out above this line are numerous error messages that I encountered when
trying to access the test class.
Many Thanks
Test script:
---------------
<?php
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\"
lang=\"en\" xml:lang=\"en\">
<head>
<title>XHTML-document
</title>
</head>
<body>";
new foo();
echo"</body>
</html>";
class foo
{
public static $bar;
public static $foo2;
public function __construct()
{
echo"<code>construct foo</code>";
echo"<p>create static instance of bar</p>";
self::$bar = new bar();
echo"<p>call hello() in bar class</p>";
self::$bar->hello();
echo"<p>call hello() in test class from Foo class through bar
class</p>";
//N.B. The following methods failed. However there is one solution...
//self::$bar->$test->hello();
//Notice: Undefined variable: test in ... on line...
//Fatal error: Cannot access empty property in ... on line...
//self::$bar->test->hello();
//Notice: Undefined property: bar::$test in ... on...
//Fatal error: Call to a member function hello() on a non-object in ...
on...
//self::$bar::$test->hello();
//Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ...
on...
//self::$bar::test->hello();
//Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ... on...
//self::$bar->self::$test->hello();
//Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ... on...
//self::$bar::self::$test->hello();
//Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in ... on...
self::$bar->getTest()->hello();
echo"<p>create static instance of foo2</p>";
self::$foo2 = new foo2();
}
}
class bar
{
//N.B. the class Test() can be instantiated as either a static or a
normal variable
//public static $test;
public $test;
public function __construct(){
echo "<code>construct bar</code>";
echo"<p>create static instance of test class in bar class </p>";
//self::$test = new test();
$this->test = new test();
}
public function hello(){
echo"<code>class:foo function:hello()</code>";
}
public function getTest(){
//echo"getanother";
//return self::$test;
return $this->test;
}
}
class test{
public function __construct(){
echo "<code>construct test</code>";
}
public function hello(){
echo "<code>class:test function:hello()</code>";
}
}
class foo2
{
public function __construct()
{
echo"<code>construct foo2</code>";
echo"<p>call hello() in foo2 class</p>";
foo::$bar->hello();
}
public function hello(){
echo"class:foo2 function:hello";
}
}
?>
Expected result:
----------------
construct foo
create static instance of bar
construct bar
create static instance of test class in bar class
construct test
call hello() in bar class
class:foo function:hello()
call hello() in test class from Foo class through bar class
class:test function:hello()
create static instance of foo2
construct foo2
call hello() in foo2 class
class:foo function:hello()
Actual result:
--------------
same as above. the code demonstrates a work around to the problem.
--
Edit bug report at http://bugs.php.net/bug.php?id=52396&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52396&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52396&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52396&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52396&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52396&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52396&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52396&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52396&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52396&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52396&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52396&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52396&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52396&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52396&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52396&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52396&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52396&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52396&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52396&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52396&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52396&r=mysqlcfg