(转载)php的类中可以不定义成员变量,直接在构造方法中使用并赋值吗?

(转载)http://s.yanghao.org/program/viewdetail.php?i=184313
php的类中可以不定义成员变量,直接在构造方法中使用并赋值吗?
class block {
  function block()
  {
  $this->iconWidth = "23";
  $this->iconHeight = "23";
  $this->bgColor = "#5B7F93";
  $this->fgColor = "#C4D3DB";
  $this->oddColor = "#F5F5F5";
  $this->evenColor = "#EFEFEF";
  $this->highlightOn = "#DEE7EB";

  $this->class = "odd";
  $this->highlightOff = $this->oddColor;
  $this->theme = THEME;
  $this->pathImg = "../themes";
  }
  ...
}

这是我看到的一个类,感觉很奇怪,它也沒有父类。

PHP基础 8 次浏览 2011-10-10 17:47
4 个回答

 

他是怎么实例化这个类的? 是什么框架里的吧?
2011-10-10 17:59 推荐: 0 次
当然可以!这有什么可奇怪的?

php 的变量不也都是不经声明就可使用的吗?
2011-10-10 19:07 推荐: 0 次

可以,php中可以,java和c++中不可以.
2011-10-10 19:29 推荐: 0 次

PHP code
class tests
{
    function testgo()
    { 
        $this-> base ='ok';
    } 
    function echos() 
    { 
        echo$this-> base; 
    } 
}
 
$t=new tests(); 
$t-> testgo(); 
$t-> echos(); 


可以的。但最好定义先吧。可定公共或私有....==这样明了些。

转载于:https://www.cnblogs.com/Robotke1/p/3139778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值