为什么我的班级需要我的会员的公共访问者? [英] Why does my class require the public accessor for my member?
问题描述
考虑:
<?php
echo" start";
class myClass < br $>
{
公共$连接;
函数getme()
{
$ this-> connection =" test";
返回$ this->连接;
}
}
$ database = new myClass();
$ mytest = $ database-> getme() ;
echo $ mytest;
echo" done";
?>
如果我删除了public $ connection以上就像
" $ connection,"我一无所获。什么都没有加载,没有开始,完成,
什么都没有,没有html标签,没有错误,nada。让我觉得这是一个错误,
但是当我使用error_reporting时,我什么都没得到。如果我把
公共修饰符放回去,我会得到结果。我以为我可以将
$连接私有并从我的访问者访问它,getme()。
我做错了什么?而且,如果我的
类错了,为什么不抱怨?谢谢。
Consider:
<?php
echo "start";
class myClass
{
public $connection;
function getme()
{
$this->connection = "test";
return $this->connection;
}
}
$database = new myClass();
$mytest = $database->getme();
echo $mytest;
echo "done";
?>
If I remove the "public $connection" above and have it just as
"$connection," I get nothing. Nothing loads, not start, done,
nothing, no html tags, no errors, nada. Make me thinks it''s an error,
but when I use error_reporting, I get nothing also. If I put the
public modifier back in I get the results. I thought I could make
$connection private and access it from my accessor, getme().
What am I doing wrong? And, why doesn''t php complain if I did my
class wrong? Thank you.
推荐答案
连接;
函数getme()
{
connection;
function getme()
{
this-> connection =" test";
return
this->connection = "test";
return
this-> ;连接;
}
}
this->connection;
}
}
这篇关于为什么我的班级需要我的会员的公共访问者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!