0

I want to create a function in a class that is available for a set of users, but that they won't be able to access. Ex:

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

So far so good, but now I want the parent class to be able to set Stuff_for_User's errors:

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

I want only Main_mess to be able to access Stuff_for_User's set_errors() method. Is that possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
localhost
  • 450
  • 2
  • 6
  • 24
  • 1
    I guess you are looking for a `friend`. See http://stackoverflow.com/questions/3707409/friend-function-in-php – The Nail Mar 07 '12 at 23:26

4 Answers4

1

No, that is not possible like that, since Main_mess is not a parent class of Stuff_for_users (and this is probably what you want, looking at what your code actually does). So set_errors has to be public if you want to call it from the outside.

1

This is not possible how you want to implement it.

Some ideas (i dont know why or how you want to do that but just ideas...):

do set_error($str,$access_key) and let $access_key be an access string only you know! let Stuff_for_user be in Extended_Stuff_for_user which has the set_error function like:

class Extended_Stuff_for_user {
  private $errors;
  private $Stuff_for_user;

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

  public function getStuffForUser() {
     return $this->Stuff_for_user;
  }
}
androidavid
  • 1,258
  • 1
  • 11
  • 20
0

Yes it possible but it can be dirty.

Like This.

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);
     }
}
Vahan
  • 524
  • 1
  • 6
  • 22
  • Yep, that's another solution. But the base problem stays the same. As a user I could just do new Main_mess() und push it to set_error. It's dirty in – androidavid Mar 07 '12 at 23:41
0

It seems that you are looking for implementation of something called friend class in php. Well .. i'm sorry to tell you this, but it is not possible.

You should look at other possible solutions to your problem.

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

}

Use it like this:

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

This should let you control the access to methods.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • PHP shouldn't call itself an OO Language then... #Failhard. – localhost Mar 08 '12 at 00:14
  • Ill try this, but I needed something sooo simple that PHP should make available. More bloat for me... – localhost Mar 08 '12 at 00:15
  • @localhost2 , it is easy to contribute new stuff .. you are welcome to submit proposals and implement them .. oh, and btw, C++ is the **only** language with friend class concept, and it is widely regarded as harmful because of tight coupling. – tereško Mar 08 '12 at 00:23
  • Yes, I'm going to change the design to something more robust and actually look at the bigger picture, instead of making a quick fix. – localhost Mar 08 '12 at 01:21