2016-07-13 7 views
-1

、私はこのようなコードの数百を持っている:

echo str_replace('x','y', $this->load1->view('something')); 

私はコードで、どこでもその行を置き換えています

echo blabla();

をし、私のコアライブラリに関数を配置しました:

function blabla(){ 
    return str_replace('x','y', $GLOBALS['this']->load1->view('something')); 
} 

rror:Fatal error...

+1

あなたが '$ this'を使っているなら、おそらく既に関数内にあります(クラス内にあるときは_method_と呼ばれます - これは' global'を使う必要はありません)が、あなたがクラスの中にいない場合まったく「$ this」は**動作しないべきです。 – FirstOne

+0

'function blabla()'はどこにありますか? '$ this'を使用しているのはクラスですか? – cmorrissey

+3

あなたは何をしようとしていますか?おそらくこれを読むべきです。 http://www.php.net/manual/en/language.oop5.basic.php – paskl

答えて

1

PHPの場合、$thisは現在のオブジェクトを指します。例えば:

class MyClass { 
    protected $attribute; 

    public function method() { 
     $this->attribute; 
    } 

    public static function staticMethod() { 
     //$this is not available here because of the static context! 
    } 
} 

$thisMyClass内で使用されています。詳細については

http://php.net/manual/en/language.oop5.basic.php

1

再:編集した質問、$thisは唯一のクラス内に存在します。

次の2つのオプションがあります。

function blabla($something){ 
    str_replace('x','y', $something); 
} 

blablah($this->load1->view('something')); 

あるいは、クラス内function blabla() {を入れて、global $thisラインをドロップします。

関連する問題