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

类与对象

时间:2011-05-16 16:00:42  来源:站内  作者:潘春会
.'::GetInstance();');
?>

I hope this saves you some effort and if anyone knows of a non-eval method to accomplish this, please share!

Thanks!

midir (02-Mar-2009 11:40)

 

 

 

There are a couple of tricks you can do with PHP's classes that programmers from C++, etc., will find very peculiar, but which can be useful.

You can create instances of classes without knowing the class name in advance, when it's in a variable:

<?php

$type
= 'cc';
$obj = new $type; // outputs "hi!"

class cc {
    function
__construct() {
        echo
'hi!';
    }
}

?>

You can also conditionally define them by wrapping them in if/else blocks etc, like so:

<?php

if (expr) {
    class
cc {
       
// version 1
   
}
} else {
    class
cc {
       
// version 2
   
}
}

?>

It makes up for PHP's lack of preprocessor directives. The caveat is that the if/else code body must have been executed before you can use the class, so you need to pay attention to the order of the code, and not use things before they're defined.

redrik at gmail dot com (01-Jan-2009 05:08)

 

 

 

Maybe someone will find these classes, which simulate enumeration, useful.
<?php
class Enum {
    protected
$self = array();
    public function
__construct( /*...*/ ) {
       
$args = func_get_args();
        for(
$i=0, $n=count($args); $i<$n; $i++ )
           
$this->add($args[$i]);
    }
   
    public function
__get( /*string*/ $name = null ) {
        return
$this
来顶一下
返回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
如何找出DHCP地址池里未使用的IP地址
如何找出DHCP地址池里
国内常用的DNS列表
国内常用的DNS列表
Linux邮件服务器软件比较
Linux邮件服务器软件比
学用纯CSS打造可折叠树状菜单
学用纯CSS打造可折叠树
相关文章
栏目更新
栏目热门