2012-10-24 35 views
1

我有一個php類Assets。在Assets之內,有各種處理資產的公共職能(緩存,縮小,合併...)。其中一項公共功能包含執行preg_replace_callback()所需的輔助功能。這個內部函數需要訪問其他公共函數之一,但我無法調用其他函數。

下面是設置:

class Assets 
{ 

    public function img($file) 
    { 

     $image['location'] = $this->image_dir.$file; 
     $image['content'] = file_get_contents($image['location']); 
     $image['hash'] = md5($image['content']); 
     $image['fileInfo'] = pathinfo($image['location']); 

     return $this->cache('img',$image); 

    } 

    public function css($content) 
    { 

     . . . 

     function parseCSS($matched){ 

      return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img() 

     } 

     $mend = preg_replace_callback(
      '#\<parse\>(.+?)\<\/parse\>#i', 
      'parseCSS', 
      $this->combined_css 
     ); 

     . . . 

    } 

} 

這是我曾嘗試:

$this->img($matched)

Error: Using $this when not in object context - Refers to $this-> inside of parseCSS()

Assets::img($matched)

Error: Using $this when not in object context - Refers to $this-> inside of img()

那麼,我怎麼能訪問公共函數$this從內部函數內?

+3

你爲什麼要在方法中包裝函數?這不符合你的想法。 – PeeHaa

+1

^這也是不好的做法。可維護性退出窗口。 – Steve

+2

兄弟,不要在函數內聲明函數。 – wesside

回答

5

這將是更爲合適:

public function css($content) 
{ 
    //. . . 
    $mend = preg_replace_callback(
     '#\<parse\>(.+?)\<\/parse\>#i', 
     array($this, 'parseCSS'), 
     $this->combined_css 
    ); 
    //. . . 
} 

public function parseCSS($matched){ 
    return $this->img($matched); // THIS LINE NEEDS TO REFERENCE function img() 
} 

你原來的做法導致parseCSS將每css被調用時定義 - 這可能會導致致命的錯誤是你永遠叫css兩次。範圍的所有問題在我修改後的例子中也更爲直接。在你原來的例子中,parseCSS是全局範圍內的一個函數,並且與你的類沒有關聯。

編輯:有效回調配方記錄在這裏:http://php.net/manual/en/language.types.callable.php

// Type 1: Simple callback 
call_user_func('my_callback_function'); 

// Type 2: Static class method call 
call_user_func(array('MyClass', 'myCallbackMethod')); 

// Type 3: Object method call 
call_user_func(array($obj, 'myCallbackMethod')); 

// Type 4: Static class method call (As of PHP 5.2.3) 
call_user_func('MyClass::myCallbackMethod'); 

// Type 5: Relative static class method call (As of PHP 5.3.0) 
call_user_func(array('B', 'parent::who')); // A 

//Type 6: Closure 
$double = function($a) { 
    return $a * 2; 
}; 

$new_numbers = array_map($double, $numbers); 

一個closure爲基礎的解決方案,也可以作爲PHP 5.4的 - 這實際上是類似於您最初的預期。

+0

我沒有意識到我可以在第二個參數中包含範圍,我試圖將'parseCSS'移入一個私有函數,但是在'preg_replace_callback'中傳遞''this-> parseCSS''作爲第二個參數並沒有您提供的代碼工作得非常好,謝謝! – VictorKilo

3

這不符合你的想法。這種「內部」的功能是在全球範圍只是另一個功能:

<?php 
class Foo 
{ 
    public function bar() 
    { 
     echo 'in bar'; 

     function baz() { 
      echo 'in baz'; 
     } 
    } 
} 

$foo = new Foo(); 
$foo->bar(); 
baz(); 

另外請注意,這將導致致命錯誤調用不止一次bar方法,更多的時候:

<?php 
class Foo 
{ 
    public function bar() 
    { 
     echo 'in bar'; 

     function baz() { 
      echo 'in baz'; 
     } 
    } 
} 

$foo = new Foo(); 
$foo->bar(); 
$foo->bar(); 
baz(); 

Fatal error: Cannot redeclare baz() (previously declared in /code/8k1

你應該去Frank Farmer answered的方式,雖然我不會使該方法public

+1

+1很好的附加信息 – JvdBerg

+0

感謝您提供的信息,我清楚地將我的一些javascript和php知識混合在一起......或缺乏這方面的知識。 – VictorKilo

相關問題