function a() { new myClass(...); } |
<?php class Object { var $property; function __construct( ) { $numargs = func_num_args(); echo "In class: Number of arguments: $numargs<br />\n"; $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br />\n"; } if ($numargs>0){ $this->property = $arg_list[0]; }else{ $this->property = 0; } } } function a() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n"; $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br />\n"; } $arg =implode(",", $arg_list); $myfunc ="return new Object($arg);"; $mynewfunc= create_function('', $myfunc); $obj = $mynewfunc(''); echo $obj->property; echo "<br /><br />\n"; } a(); a(1, 2, 3); a(1, 2, 3, 4); ?> |
原帖由 a_coder 于 2007-10-23 10:20 發(fā)表
2樓的等于沒(méi)說(shuō),那些東西無(wú)法解決這個(gè)問(wèn)題,請(qǐng)看清楚問(wèn)題再說(shuō),手冊(cè)我讀的比你多。
原帖由 a_coder 于 2007-10-23 10:20 發(fā)表
2樓的等于沒(méi)說(shuō),那些東西無(wú)法解決這個(gè)問(wèn)題,請(qǐng)看清楚問(wèn)題再說(shuō),手冊(cè)我讀的比你多。
3樓的說(shuō)數(shù)組,這個(gè)辦法是沒(méi)有辦法的笨辦法,代碼會(huì)變得很不易閱讀。至于你說(shuō)的羊肉串我不明白什么意思,是不是就是根據(jù)參數(shù)個(gè) ...
<? $data = array('foo'=>'bar', 'baz'=>'boom', 'cow'=>'milk', 'php'=>'hypertext processor'); $me=http_build_query($data); // foo=bar&baz=boom&cow=milk&php=hypertext+processor //這樣,你得到了一個(gè)羊肉竄,就是$me。把它傳到你想要傳到的地方。 class h { var $t; function __construct($me) { $this->h($me); } function h($me) { //要使用的時(shí)候,還原成數(shù)組或其它的。老兄好像不喜數(shù)組,那就轉(zhuǎn)成獨(dú)立變量吧。 $a=explode('&',$me); foreach($a as $b) { $c=explode('=',$b); $k=$c[0]; $$k=$c[1]; } $this->t=$foo;//拿一個(gè)出來(lái)測(cè)試 } } function hello($me) { $m=new h($me); echo $m->t; } hello($me); ?> |
new myClass( $a, $b ), new myClass( $a, $b, $c ), new myClass()。以此類(lèi)推。
原帖由 a_coder 于 2007-10-23 13:48 發(fā)表
8樓的辦法,有點(diǎn)復(fù)雜,一個(gè)比較簡(jiǎn)單的辦法是把 myClass 不定義 PHP 默認(rèn)的構(gòu)造函數(shù) __construct,定義一個(gè)自定義的具有構(gòu)造函數(shù)相同作用的方法 init,然后在func中
$myClass = new myClass;
call_user_func ...
但是如果即沒(méi)有參數(shù),但是init確實(shí)還需要執(zhí)行呢。
class fu { ............. function build($class_name,$param=null) { $f_path =PATH_INCLUDE.$class_name.EXT; if($this->_is_exist(PATH_INCLUDE,$class_name.EXT)) { if(!@is_object($this->$class_name)) { require($f_path); $str = '$this->$class_name =new $class_name('; if (is_array($param)) { foreach ($param as $key) { if (!empty($key)) { $str .= '\''.$key.'\','; } } $str = substr($str, 0, -1); } $str .= ');'; eval($str); $this->obj[$class_name] =&$this->$class_name; $this->$class_name->fu = &$this; if (method_exists($this->$class_name,'init')) { $this->$class_name->init(); } } return $this->$class_name; } else { $this->fatal_error('file:'.$f_path.' not found!<BR>'); } } |
原帖由 dz902 于 2007-10-23 13:33 發(fā)表
我不懂你的意思。constructor 可以有默認(rèn)參數(shù),init 也可以。
constructor = 至少 2 種情況 (有參,無(wú)參)
constructor + init = 至少 4 種情況 (con 有參 init 有參,con 有參 init 無(wú)參,con 無(wú)參 ini ...
<?php class a { public function __construct() { $arguments = get_func_args(); call_user_func_array( array(&$this, 'init'), $arguments); } public function init( $a, $b ) { echo $a.$b; } } function load() { $inst = new a; $args = get_func_args(); call_user_func_array( array(&$inst, 'init'), $args ); } ?> |
原帖由 achieverain 于 2007-10-23 13:43 發(fā)表
看來(lái)有懸賞的帖子回答就是踴躍.我也湊湊熱鬧
LZ可以看看PHP實(shí)現(xiàn) 工廠模式
這里是我的一段正在運(yùn)行中的代碼
class fu
{
.............
function build($class_name,$param=nul ...
原帖由 sunnyfun 于 2007-10-23 13:51 發(fā)表
說(shuō)了半天不就是想new的時(shí)候自動(dòng)初始話(huà)一下嘛,要知道__construct也是一個(gè)函數(shù):
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // PHP CODE Highliting for CU by dZ902 <? function __construct($auto_init = FALSE, ...) { // ... if ($auto_init == TRUE) { call_user_func_array( array(&$this, 'init'), $args); return; } else { // do construction } } ?> |
歡迎光臨 Chinaunix (http://www.72891.cn/) | Powered by Discuz! X3.2 |