2012-03-07 76 views
0

我想創建一個功能的類可用於一組用戶,但他們將無法訪問。例如:類功能只適用於「父」 - PHP

class Stuff_for_user { 
    private $errors; 
    /* 
    * private $errors gets modified by private functions 
    */ 

    public function get_errors(){ // This is for users to display errors. 
      return $this->errors; 
    } 

    /*something here...*/ function set_errors($str){ 
      $this->errors = $str; 
    } 
} 

到目前爲止好,但現在我想父類能夠設置Stuff_for_User的錯誤:

class Main_mess { 
    public index(){ 
      $user_available_data = new Stuff_for_user(); 

      if($big_error) 
       $user_available_data->set_errors("BIG ERROR!!!"); 

      $this->send_to_users($user_available_data); 
    } 
} 

我只想Main_mess能夠訪問Stuff_for_Userset_errors()方法。那可能嗎?

+1

我想你正在尋找一個「朋友」。請參閱http://stackoverflow.com/questions/3707409/friend-function-in-php – 2012-03-07 23:26:50

回答

0

看來你正在尋找在php中實現一個叫friend class的東西。那麼..我很抱歉告訴你這個,但這是不可能的

你應該看看你的問題的其他可能的解決方案。

class SecureContainer{ 

    protected $user = null; 
    protected $target = null; 

    public function __construct($target, $user) 
    { 
     $this->target = $target; 
     $this->user = $user; 
    } 

    public function __call($method, $arguments) 
    { 
     if ($this->user->isAllowed(getType($this->target), $method)) 
     { 
     return call_user_func_array( 
      array($this->target, $method), $arguments); 
     } 
    } 

} 

使用方法如下:

$something = new UnsecureSomething; 
$user = new User($uid); 
$something = new SecureContainer($something, $user); 

這應該讓你控制訪問方法。

+0

PHP不應該稱自己是OO語言,那麼...... #Failhard。 – localhost 2012-03-08 00:14:00

+0

我試試這個,但我需要一些簡單的東西讓PHP變得可用。更多的膨脹對我來說... – localhost 2012-03-08 00:15:11

+0

@ localhost2,很容易貢獻新的東西..歡迎您提交建議和實施他們..哦,順便說一句,C++是唯一的**語言與朋友類的概念,並且由於緊密耦合而被廣泛認爲是有害的。 – 2012-03-08 00:23:04

1

不,那是不可能的,因爲Main_mess不是父類Stuff_for_users(這可能是你想要的,看你的代碼實際上是什麼)。所以set_errors必須是public如果你想從外面打電話。

1

這是不可能的,你想如何實現它。

的一些想法(我不知道爲什麼還是要如何做,但只是想法...):

set_error($str,$access_key),讓$ access_key是訪問字符串只有你自己知道! 讓Stuff_for_user在Extended_Stuff_for_user它具有set_error功能,如:

class Extended_Stuff_for_user { 
    private $errors; 
    private $Stuff_for_user; 

    public function set_errors() { 
     /* ... */ 
    } 

    public function getStuffForUser() { 
    return $this->Stuff_for_user; 
    } 
} 
+0

您是否知道您的解決方案正在破壞SRP和LSP? – 2012-03-07 23:43:59

+0

@teresko:在這種情況下,它不介意,它無論如何都很髒 – androidavid 2012-03-07 23:45:35

0

是有可能,但也可以是骯髒的。

喜歡這個。

class Stuff_for_user { 
    private $errors; 
    /* 
    * private $errors gets modified by private functions 
    */ 

    public function get_errors(){ // This is for users to display errors. 
      return $this->errors; 
    } 

     /* 
      This way the child classes of Main will able be to use the set_errors function; 
     */ 
    function set_errors($class,$str){ 
      if($class instanceof Main_mess) 
      { 
      $this->errors = $str; 
      } 

     /* 
     AndThis way the only Main_mess will be able; 
     */ 
     function set_errors($class,$str){ 
      if(get_class($class)=="Main_mess") 
      { 
      $this->errors = $str; 
      } 

    } 


class Main_mess { 
    public index(){ 
      $user_available_data = new Stuff_for_user(); 

      if($big_error) 
       $user_available_data->set_errors($this,"BIG ERROR!!!"); 

      $this->send_to_users($user_available_data); 
    } 
} 
+0

是的,這是另一種解決方案。但基本問題保持不變。作爲一個用戶,我可以做新的Main_mess()並將其推送到set_error。它很髒 – androidavid 2012-03-07 23:41:19