dsfds656545 2013-04-22 07:54
浏览 17
已采纳

属性是否默认声明为私有? [关闭]

I have the property $value which gives different error messages depending on whether I prepend private to the variable declaration or not.

Class Node{
        $value = NULL;
        public function __construct($value){
                $this->value = $value;
        }
}

$node = new Node('hello');
echo $node->value;

PHP Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/ubuntu/new.php on line 4

However if I replace $value = NULL; with private $value = NULL; I get a different error message:

PHP Fatal error: Cannot access private property Node::$value in /home/ubuntu/new.php on line 11

I thought omitting the term private would automatically make the property private?

EDIT

I understand now that properties are public by default. How come I get an error when declaring the property $value without the public prepended to it?

  • 写回答

2条回答 默认 最新

  • dougu3591 2013-04-22 07:59
    关注

    You got firs error before

    echo $node->value;
    

    Put class code without calling it and you'll get same error.

    You just have syntax error on line:

     $value = NULL;
    

    Replace with:

    var $value = NULL;
    

    UPDATE

    But this is wrong solution just to show where are your problem. Always set variable visibility one of:

    • private
    • protected
    • public
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据