Consider the following code. What change must be made to the class for the code to work as written?
class Magic {
protected $v = array("a" => 1, "b" => 2, "c" => 3);
public function __get($v) {
return $this->v[$v];
}
}
$m = new Magic();
$m->d[] = 4;
echo $m->d[0];
Suggested Answer: D
Get Premium 200-710 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
36 comments · newest first
DO
Dorcas
11 months ago
I thought it worked fine as is, but I guess not!
JA
Jame
11 months ago
Wait, why would you need a __set? Isn't it just a getter?
NI
Nickolas
11 months ago
Totally agree, option B is the way to go!
JE
Jeannetta
11 months ago
You need to add a __set method for that to work.
KR
Kris
11 months ago
Making __get static? That's a no-go.
FI
Filiberto
12 months ago
I’m confused about the static part in option E. I don’t recall that being necessary for this kind of situation.
NE
Nelida
12 months ago
This question feels familiar. I think we might have done a similar one where we had to implement __set for dynamic properties.
JU
Julio
1 year ago
I'm not entirely sure, but I remember something about needing to rewrite __get to return a reference. Maybe option D?
VI
Virgilio
1 year ago
I think we need to add a __set method to handle the assignment to $m->d. That seems to make sense based on what we practiced.
CA
Cathrine
1 year ago
I'm not sure about this one. I'll need to review the PHP magic methods and how they work with arrays before I can decide on the best approach.
BR
Brinda
1 year ago
I'm pretty confident that the answer is B. We need to add an __set method to the class to allow for setting new properties.
RE
Reed
1 year ago
Okay, I think I've got it. The issue is that we're trying to access a property that doesn't exist in the $v array, so we need to add a way to handle that.
JO
Joseph
1 year ago
Hmm, I'm a bit confused by the question. I'm not sure what change needs to be made to the class for the code to work as written.
HE
Hester
1 year ago
This looks like a tricky one. I'll need to carefully read through the code and think about how the __get method works.
LI
Lili
1 year ago
I see your point, Mari. Maybe we are overthinking it.
MA
Mari
1 year ago
I think the correct answer is A) Nothing, this code works just fine.
SH
Sherell
1 year ago
Wait, this isn't a magic trick question? Darn, and here I was hoping to pull a rabbit out of my hat. Oh well, I'll just go with option B and see if I can make a few galleons appear instead.
DI
Dierdre
1 year ago
I'm curious to see if this change will work. Good luck!
DE
Denae
1 year ago
That's a good choice. Let's see if you can make those galleons appear!
RU
Ruth
1 year ago
Yeah, adding the __set method should do the trick.
FE
Felix
1 year ago
Option B) Add __set method doing $this->v[$var] = $val
VI
Vinnie
1 year ago
I think option B is the way to go.
CO
Corrie
1 year ago
But wouldn't adding a reference in __get cause potential issues?
MA
Margarita
1 year ago
This question is making my brain hurt. I'm just going to go with the option that sounds the most complicated - let's go with E and make __get static. That'll show 'em!
DE
Denise
1 year ago
I'm feeling a bit lost here. Maybe I should've paid more attention in my PHP classes... Anyway, I'll go with C and hope for the best!
KA
Kaycee
1 year ago
B) Add __set method doing $this->v[$var] = $val
BR
Brittney
1 year ago
C) Rewrite __get as: public function __get(&$v)
SH
Sharen
1 year ago
Ha! This is a classic trick question. The answer is clearly A, the code works just fine as is. Those other options are just trying to confuse us.
LI
Lili
1 year ago
I disagree, I believe the answer is D) Rewrite __get as: public function &__get($v)
TA
Tamekia
1 year ago
Hmm, I'm not so sure. I'm leaning towards D, where we need to use a reference in the __get method to allow modifying the array element.
TI
Tijuana
1 year ago
User 2: Yeah, that makes sense. It would allow modifying the array element.
DE
Desmond
1 year ago
User 1: I think D is the correct option, we need to use a reference in the __get method.
CO
Corrie
1 year ago
I think the answer is B) Add __set method doing $this->v[$var] = $val
DE
Desire
1 year ago
Wow, this is a tricky one! I think the answer is B, since we need to add an __set method to allow writing to the array.
RI
Rima
1 year ago
I agree, adding an __set method would allow writing to the array.
CR
Crissy
1 year ago
I think the answer is B, we need to add an __set method.