Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类函数仅适用于;母公司PHP_Php_Oop_Class - Fatal编程技术网

类函数仅适用于;母公司PHP

类函数仅适用于;母公司PHP,php,oop,class,Php,Oop,Class,我想在一个类中创建一个函数,该类可供一组用户使用,但他们无法访问。例: 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

我想在一个类中创建一个函数,该类可供一组用户使用,但他们无法访问。例:

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\u:

 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\u mess
能够访问用户
set\u errors()
方法的
Stuff\u。这可能吗?

不,这是不可能的,因为
Main\u mess
不是用户的
Stuff\u的父类(这可能是您想要的,看看您的代码实际做了什么)。因此,如果你想从外部调用它,设置错误必须是公共的。

这是不可能的

一些想法(我不知道你为什么或如何想这样做,但只是想法…):

设置错误($str,$access\u key)
并让$access\u key成为只有您知道的访问字符串! 让\u user的Stuff\u位于
扩展的\u Stuff\u for \u user
中,该函数具有如下设置错误功能:

class Extended_Stuff_for_user {
  private $errors;
  private $Stuff_for_user;

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

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

是的,这是可能的,但它可能很脏

像这样

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);
     }
}

看起来您正在寻找php中所称的实现。好。。很抱歉告诉您,但这是不可能的

你应该考虑其他可能的解决方法

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 );

这应该可以让您控制对方法的访问。

我想您正在寻找一位
朋友。是的,这是另一个解决方案。但基本问题依然存在。作为一个用户,我可以只做新的Main_mess()并将其推到set_error。Ar是肮脏的,你知道你的解决方案破坏了SRP和LSP吗?PHP不应该称自己为OO语言#我会试试这个,但是我需要一些PHP应该提供的非常简单的东西。对我来说更多的膨胀…@localhost2,贡献新东西很容易。。欢迎您提交建议并付诸实施。。哦,而且BTW,C++是唯一的朋友类概念语言,它被广泛认为是有害的,因为紧密耦合。是的,我将改变设计到更健壮的东西,实际上看更大的图片,而不是快速修复。