作者HuangJC (吹笛牧童)
看板PHP
標題Re: [請益] 有沒有法子把 $this pass 掉
時間Sun Jul 13 19:34:15 2008
完全成功了,謝謝大家 XD
;;
本來我還想寫一個 parser 咧
自動把 *.thtml 轉成 *.php
只做一件事:把 $this 濾掉
這樣想太複雜了
1.如果每次都要 parse,那不如存檔起來 cache
phpbb3 比起 phpbb2,就是多了好多 cache file
這或許值得學
2.存檔在 linux 下必需要有權限,同事提醒我這件事
所以我也想過能不能在變數裏執行
$file=讀('demo.thtml');
$file=濾($file);
//不存檔
eval($file);
想得很簡單,碰壁了..
原來 eval 在執行時,不需要最外側的 <?php ?>
那是不是我把過濾函式寫完整一點
把最外側摘掉就OK 了呢?
我覺得這樣會無法在時間內交作業
所以很沒骨氣的打退堂鼓了 :P
所以 demo.thtml => demo.php 這件事,我是手動做的
現在我成功了,但還是產生新的問題
(也可以開心的說,是新的學習)
其實 demo.thtml 是更複雜點的
HTML DEMO PART<BR/>
<?php
$session->read();
$this->here();
?>
如上,也就是說,我原本就有生一個偽造的 session class
(看吶,我不熟 CakePhp,可是我偽造時也一直模仿,session,很熟悉的變數名吧 XD)
放在 my_try.php 裏
class Session
{
function read()
{
//cheat code
}
}
$session=new Session;
這樣我就擁有 $session,同時也能應付 $session->read() 這樣的要求
但這是指當改造的 demo.php 被直接載入時
當我改版使用 demo.thtml ,在 class 內部載入
發生的問題是 $sesson 並不存在;因為我現在是在 class 內想取用全域變數了!!
所以 my_try.php 改版其實是這樣的
class Cheat
{
var $session =new Session;//本來想這樣用,不行,因為這得用 $this->session 取用
function here()
{
//cheat code
}
function Cheat
{
global $session; //我得這樣,要求取用 global 產生的那個
//上面是舊版,新版更有趣
$session=new Session; //直接在內部產生這個物件,
//不必在全域產生再宣告 global,反正外面用不到它
require 'demo.thtml';
}
}
如上,問題就解決了
回過頭來,今晚看懂部份 Cake,我完全就是在模仿它的邏輯 XD
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.59.179.152
※ 編輯: HuangJC 來自: 61.59.179.152 (07/14 02:48)