 |
อยากทราบวิธีการใช้ class แบบเหมือน extends แต่ไม่ได้ extends |
|
 |
|
|
 |
 |
|
อ้อ ผมลอง public function __call($name, $arg) แล้วนะครับ
มันก็ใช้ได้ ถ้า $html->br(); ตัวเดียวเฉยๆ
แต่ถ้ามี $html->br(); หลายๆตัว หรือ $html->br(array('class'=>'clear'));
มันจะกลายเป็น array แทนที่จะออกมาเป็น <BR class="clear">
__call() ใช้ไม่ได้ผลครับ 
|
 |
 |
 |
 |
Date :
2010-01-09 18:40:37 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใช้แบบนี้ครับ
<?php
$HTML_CLASS = 'H1';
abstract class H{
/**
* print Class name
*/
abstract function e();
/**
* @return string Class name
*/
abstract function r();
}
class H1 extends H{
function e(){echo 'H1';}
function r(){return 'H1';}
}
class H2 extends H{
function e(){echo 'H2';}
function r(){return 'H2';}
}
/**
*
* @global string $HTML_CLASS
* @return H
*/
function html(){
global $HTML_CLASS;
return new $HTML_CLASS;
}
$h = html();
$h->e();
?>
|
 |
 |
 |
 |
Date :
2010-01-09 19:16:52 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เดี๋ยวคืนนี้กลับมาลองครับ ขอบคุณไว้ก่อน
|
 |
 |
 |
 |
Date :
2010-01-09 19:21:53 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อีกแบบนึงครับ
<?php
$HTML_CLASS = 'H1';
class H{
private $obj;
public function __construct() {
global $HTML_CLASS;
$this->obj = new $HTML_CLASS;
}
/*
public function e(){
$args=func_get_args(); return call_user_method_array(__FUNCTION__,$this->obj,$args);
}
public function r(){
$args=func_get_args(); return call_user_method_array(__FUNCTION__,$this->obj,$args);
}
public function p($s){
$args=func_get_args(); return call_user_method_array(__FUNCTION__,$this->obj,$args);
}
*/
public function __call($name, $params){
call_user_method_array($name, $this->obj, $params);
}
}
class H1 extends H{
public function __construct(){}
public function e(){echo 'H1';}
public function r(){return 'H1';}
public function p($s){echo $s.'__H1';}
}
class H2 extends H{
public function __construct(){}
public function e(){echo 'H2';}
public function r(){return 'H2';}
public function p($s){echo $s.'__H2';}
}
$h = new H();
$h->e();
$h->p('ddd');
?>
|
 |
 |
 |
 |
Date :
2010-01-09 20:28:17 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างล่างไม่เวิร์คอะครับ มันเงียบหายไปเลยแล้วก็ขึ้น The connection was reset
ตัวอย่างแรกจะติดตรงที่ ต้องสร้าง function เหมือนๆกันในทุกๆ class html
ดูโค้ดอีกที
Code (PHP)
class html {
private $tag;
public function __construct() {
global $cfg;
$this->tag = new $cfg['html_ver']; // ควรเป็น new html4;
}// __construct
public function __call($name, $arguments) {
global $cfg;
call_user_method_array($name, $this->tag, $arguments);
//return $newhtml->$name($arguments);
}// __call
}
html.php
Code (PHP)
class html4 extends html {
/**
* ทุกๆ function $option ให้ใส่เป็นแบบ array เช่น $html->tag(array('class'=>'classname'));
*/
public function br($option='') {
$output = "<BR";
if ($option != null) {
foreach ($option as $attr=>$val) {
$output .= " ".$attr."=\"".$val."\"";
}
}
$output .= ">";
return $output;
}// br
html4.php
เขียน $html = new html();
echo $html->br();
เดี้ยงเลยครับ
|
 |
 |
 |
 |
Date :
2010-01-09 22:17:26 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ประกาศ public function __construct(){} ใน class html4 ครับ ถ้าไม่ประกาศมันจะ recursive ครับ
ดูอีกที html4 ไม่จำเป็นต้อง extends html ครับ ผมเขียนเกินไปครับ
ตรง
call_user_method_array($name, $this->tag, $arguments);
แก้นิดนึงเป็น
return call_user_method_array($name, $this->tag, $arguments);
จะทำให้ใช้งาน function return ได้ครับ
|
 |
 |
 |
 |
Date :
2010-01-09 22:59:28 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จ๊าบบบบ ใช้ได้แล้ว (เท่าที่ลองดูหลายๆแบบนะ <br> <br id="id"> ,...)
ขอบคุณคร้าบ
return call_... ผมก็กำลังจะมาบอกพอดี 
|
 |
 |
 |
 |
Date :
2010-01-09 23:56:09 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|