search

Home  >  Q&A  >  body text

For the third access method, I get an error locally.

echo '3.Object::Class constant name:'.(new demo)::siteName.'<br>';

Error message:


Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM), expecting ',' or ';' in E:\web\phpstudy\WWW\class\oopself.php on line 18

php version is 5.4


哥特哥特2435 days ago1266

reply all(1)I'll reply

  • 哥特

    哥特2018-05-02 22:12:42


    <?php

    header("Content-type: text/html; charset=utf-8");

    class demo{

    const siteName = 'php中文网';

    const domain = <<< 'EOT'

    <a href="javascript:;">www.php.cn</ a>

    EOT;

    public function getSiteName(){

    return self::siteName;

    }

    }

    //Method 1: Class name: Class constant name.

    echo '1.Class name::Class constant name'.demo::siteName.demo::domain.'<br>';

    //Method 2: Class variable: Class constant name must be run in PHP5.3 or above.

    $className = 'demo';

    echo '2. Class variable::Class constant name:'.$className::siteName.'<br>';

    //Method 3: Use the object of the current class to access class constants.

    echo '3.Object::Class constant name:'.(new demo)::siteName.'<br>';

    //Method 4: Use the method of strangulation to access class constants indirectly.

    echo '4.Object->Method():'.(new demo)->getSiteName();

    ?>


    reply
    0
  • Cancelreply