Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-18-2010, 09:49 PM   PM User | #1
crmpicco
Senior Coder

 
crmpicco's Avatar
 
Join Date: Jan 2005
Location: Mauchline, Scotland
Posts: 1,091
Thanks: 15
Thanked 1 Time in 1 Post
crmpicco has a little shameless behaviour in the past
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
crmpicco is offline   Reply With Quote
Old 07-19-2010, 03:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 07-25-2010, 07:49 PM   PM User | #3
crmpicco
Senior Coder

 
crmpicco's Avatar
 
Join Date: Jan 2005
Location: Mauchline, Scotland
Posts: 1,091
Thanks: 15
Thanked 1 Time in 1 Post
crmpicco has a little shameless behaviour in the past
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 ;-)
crmpicco is offline   Reply With Quote
Reply

Bookmarks

Tags
certification, exam, oop, php, zend

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:53 PM.


Advertisement
Log in to turn off these ads.