[請益] class內function呼叫問題已刪文

看板PHP作者 (只有音樂相伴)時間6月前 (2023/10/28 21:14), 6月前編輯推噓0(001)
留言1則, 1人參與, 6月前最新討論串1/1
我寫了一個class 在外面產生class物件呼叫input函式 --------------------------------------------- $defaults = array('price' => '5.00'); $form = new FormHelper($defaults); $form->input('text', ['name' => 'dish_name']); --------------------------------------------- 執行結果有顯示input function內的XXX 但tag function裡的YYY就沒有顯示 顯示tag並沒有被呼叫 為什麼呢 有人可以解釋嗎 thanks --------以下是物件程式碼 <?php class FormHelper { protected $values = array(); public function __construct($values = array()){ if ($_SERVER['REQUEST_METHOD']== 'POST'){ $this->values = $_POST; }else{ $this->values = $values; } } public function input($type, $attributes = array(), $isMultiple = false){ $attributes['type'] = $type; print "XXX"; return this->tag('input',$attributes, $isMultiple); } public function tag($tag, $attributes = array(), $isMultiple = false){ print "YYY"; return "<$tag {$this->attributes($attributes, $isMultiple)} />"; } protected function attributes($attributes, $isMultiple, $valueAttribute = true){ $tmp = array(); if($valueAttribute && isset($attributes['name']) && array_key_exists($attributes['name'], $this->values)){ $attributes['value'] = $this->values[$attributes['name']]; } foreach ($attributes as $k => $v){ if(is_bool($v)){ if($v) { $tmp[] = $this->encode($k);} } else{ $value = this->encode($v); if($isMultiple && ($k == 'name')){ $value .='[]'; } $tmp[] = "$k=\"$value\""; } } return implode(' ', $tmp); } public function encode($s) { return htmlentities($s); } } ?> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.255.67.167 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/PHP/M.1698498878.A.084.html ※ 編輯: afred (111.255.67.167 臺灣), 10/28/2023 21:16:25 ※ 編輯: afred (111.255.67.167 臺灣), 10/28/2023 21:17:09

10/28 21:31, 6月前 , 1F
this 前面缺了 $
10/28 21:31, 1F
文章代碼(AID): #1bFGa-24 (PHP)
文章代碼(AID): #1bFGa-24 (PHP)