免费邮箱 |加入收藏 | 会员中心 | 我要投稿 | RSS
您当前的位置:首页 > PHP专区 > PHP教程

类与对象

时间:2011-05-16 16:00:42  来源:站内  作者:潘春会

 

 

For real quick and dirty one-liner anonymous objects, just cast an associative array:

<?php

$obj
= (object) array('foo' => 'bar', 'property' => 'value');

echo
$obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'

?>

... no need to create a new class or function to accomplish it.

ranema at ubuntu dot polarhome dot com (30-Mar-2008 11:49)

 

 

 

Sometimes you just forget to close handles, links, etc and sometimes you are just lazy to do that. PHP 5 OOP can do it automatically by using destructors:

<?php
class MySqlDriver {
   private
$_Link;

   public function
__construct( <...> ) {
     
$this->_Link = mysql_connect( <...> );
   }

  
// this will be called automatically at the end of scope
  
public function __destruct() {
     
mysql_close( $this->_Link );
   }
}

$_gLink = new MySqlDriver( <...> );
// and you don't need to close the link manually
?>

osculabond at gmail dot com (07-Oct-2006 02:20)

 

 

 

A better way to simulate an enum in php5:

<?php
final class Days {
    const
Sunday     = 0x00000001;
    const
Monday     = 0x00000010;
    const
Tuesday    = 0x00000100;
    const
Wednesday = 0x00001000;
    const
Thursday  = 0x00010000;
    const
Friday    = 0x00100000;
    const
Saturday  = 0x01000000;
    const
Unknown    =
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门