Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,093
    Thanks
    15
    Thanked 1 Time in 1 Post

    You cannot modify private member variables of a different class (Zend Cert)

    Hey,

    I don't have PHP installed on the machine i'm on when coming up against this question in my Zend Cert study. Can anyone advise?

    Q: What are the values of $a in $obj_one and $obj_two when this script is executed?

    PHP Code:
    <?php
    class myClass {
      private 
    $a;

      public function 
    __construct() {
        
    $this->10;
      }

      public function 
    printValue() {
        print 
    "The Value is: {$this->a}\n";
      }

      public function 
    changeValue($val$obj null) {
        if(
    is_null($obj)) {
          
    $this->$val;
        } else {
          
    $obj->$val;
        }
      }

      public function 
    getValue() {
        return 
    $this->a;
      }
    }

    $obj_one = new myClass();
    $obj_two = new myClass();

    $obj_one->changeValue(20$obj_two);
    $obj_two->changeValue($obj_two->getValue(), $obj_one);

    $obj_two->printValue();
    $obj_one->printValue();

    ?>
    Answers:
    10,20
    You cannot modify private member variables of a different class
    20,20
    10,10
    20,10

    I believe it should be "You cannot modify private member variables of a different class". Any thoughts?

    Picco

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, its 20, 20. Object context doesn't hinder PHP's scope visibility, so either instance can be modified in any scope. See this link for visibility from other objects.
    This makes sense though. When two types of objects are known to each other, they should logically know what each other know, regardless of if its public or lesser. A better way of writing it for clarity would be:
    PHP Code:
    public function changeValue($valMyClass $obj null
    Keyword 'self' can also be used as typehint. This is easier to see since we know that both 'this' and 'obj' are MyClass objects, so eachother should know what the other can do.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,093
    Thanks
    15
    Thanked 1 Time in 1 Post
    You're spot on Fou-Lu, just tested it locally and you are 100% correct.
    Code:
    The Value is: 20 The Value is: 20
    Thanks for the link too.

    BTW: what is your name, I keep calling you Fou-Lu ;-)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •