.'::GetInstance();');
?>
I hope this saves you some effort and if anyone knows of a non-eval method to accomplish this, please share!
Thanks!
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; 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 {
}
} else {
class cc {
}
}
?>
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.
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( $name = null ) {
return $this
2/10 首页 上一页 1 2 3 4 5 6 下一页 尾页 |