Saturday, March 12, 2011

PHP 5.3 Usage Notes - Static Constructor and Property Initialization Errors


1. You cannot call a function statically with the same class name being a backward compatible constructor. The following won't work.

<?php
class ctest
{
        static function ctest()
        {
                echo "test";
        }
}
ctest::ctest();
Error Expected:
Fatal error: Constructor ctest::ctest() cannot be static in /var/www/html/test.php

2. You cannot assign an expression, a function or an object to a property. The following won't work.


<?php
class ctest
{
        public static $mytest = memory_get_usage();
        static function test()
        {
                echo "test";
        }
}
ctest::test();
class ctest
{
        public $mytest = new StdClass();
        static function test()
        {
                echo "test";
        }
}
ctest::test(); 
class ctest
{
        static $mytest = (true) ? "not false" : "not true";
        static function test()
        {
                echo "test";
        }
}
ctest::test();
Error Expected:
Parse error: syntax error, unexpected '(', expecting ',' or ';'

Note: This possibly exists even to earlier versions of PHP. Also, the similar limitation of static properties which cannot be initialized using a non-literal or non-constant applies here as well.


  • Related Links Widget for Blogspot

No comments: