Page 1 of 1
Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 5:34 pm
by iG9
First let me say I tried to search for the answer but the search kept ignoring the term "array" even with the plus sign before it so sorry if this is common knowledge...
Here we have a couple of objects:
Code: Select all
class Shapes {
public $color;
public $sides;
public $lengths = array();
public function access($action,$prop,$value=NULL) {
switch ($action) {
case 'read':
$value = $this->$prop;
return($value);
break;
case 'write':
$this->$prop = $value;
return(1);
break;
}
}
}
class Polygon extends Shapes {
public function circumference() {
$circumference = 0;
foreach ($this->lengths as $key => $value) {
$circumference += $value;
}
return($circumference);
}
}
Now, usually I can modify their properties like so:
Code: Select all
$polygon = new Polygon;
$polygon->access(write,sides,5);
However, when I try to change the $lengths array, like so:
Code: Select all
$polygon->access(write,lengths[],4);
I get this, "syntax error, unexpected '[' in /users/jhoward1/shapes.php".
Do I need to alter the access() method somehow? All help deeply appreciated.
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 5:57 pm
by requinix
1.
Strings have quotes around them. If they do not have quotes they are not strings.
"write", "sides", and "lengths" are supposed to be strings.
2. There is no easy way to modify access() to make it do what you want.
3. This access() function is not needed. Rather silly, really.
If $color, $sides, and $lengths are publicly available (which they are) then just modify them directly.
Code: Select all
$polygon->sides = 5;
$polygon->lengths[] = 4;
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 6:00 pm
by pickle
I agree that the function is pretty much superfluous. It would be useful only if the variables were private.
However, as for your original question: it's unclear to me exactly what you want $lengths to be after this operation. Do you want it to equal the number 4? Do you want to append a new element to the end of it with value 4? Do you want to truncate the length of $lengths to be only 4 elements?
The reason you're getting an error is because you need to quote your arguments. Additionally, you don't need to put [] beside "lengths".
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 6:22 pm
by iG9
Originally I was quoting the strings. When I noticed it worked without them I started leaving them out. I'm still learning OOPHP and I thought the syntax might have been different.
What I want to happen is for the value "4" to be appended to the array in the first available slot. I actually tried quoting it, i.e.
Code: Select all
$polygon->access("write","lengths[]",4);
but it doesn't modify the array, it just stops throwing errors. If I don't put the [] after lengths it just makes the variable a scalar with a value of 4, which isn't what I'm going for. I realize the access() method is superfluous but if the variables were private it'd be of use right? So there must be a way to do this. Sorry if the initial explanation was unclear.
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 6:59 pm
by requinix
The simplest thing would be to
Code: Select all
$polygon->access("write","lengths",4);
and then put some logic into access() that
- overwrites if the variable is a string/int/etc
- appends if the variable is an array
Code: Select all
case 'write':
if (is_array($this->$prop)) {
$this->$prop[] = $value;
} else {
$this->$prop = $value;
}
You'd want a "reset" or "clear" action too - to clear out arrays.
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 8:03 pm
by iG9
Thanks for the idea, and that seems like it should work, but it gives this error, "Fatal error: Cannot use [] for reading" on this line
Trying to debug but I'm amazed this is so difficult.
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 8:06 pm
by Weirdan
Re: Modifying an Object's Array Property
Posted: Wed Jun 10, 2009 8:24 pm
by iG9
@Weirdan Thanks for the idea, but that just sets $lengths to a scalar. So if I do this:
Code: Select all
$polygon->access("write","lengths",4);
$polygon->access("write","lengths",6);
$polygon->access("write","lengths",3);
echo $polygon->lengths;
I just get "3". I figured out a way to get this to work, sort of, but I don't like it:
Code: Select all
case 'write':
if ($prop == "lengths") {
$this->lengths[] = $value;
return(1);
} else {
$this->$prop = $value;
return(1);
}
break;
I'd still love to find a way to do this generally, without having to know what the array was called.
Re: Modifying an Object's Array Property
Posted: Thu Jun 11, 2009 2:57 am
by Weirdan
iG9 wrote:@Weirdan Thanks for the idea, but that just sets $lengths to a scalar.
No, it does work as intended. Post your code.
Re: Modifying an Object's Array Property
Posted: Thu Jun 11, 2009 4:04 am
by globezone
try this...
Code: Select all
case 'write':
//
$tmp =& $this->$prop;
//
if(is_array($tmp))
$tmp[] = $value;
else
$tmp = $value;
return(1);
break;
Re: Modifying an Object's Array Property
Posted: Thu Jun 11, 2009 1:43 pm
by iG9
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
@Weirdan
Code: Select all
class Shapes {
public $color;
public $sides;
public $lengths = array();
public function access($action,$prop,$value=NULL) {
switch ($action) {
case 'read':
$value = $this->$prop;
return($value);
break;
case 'write':
if (is_array($prop)) {
$this->{$prop}[] = $value;
return(1);
} else {
$this->$prop = $value;
return(1);
}
break;
}
}
}
class Polygon extends Shapes {
public function circumference() {
$circumference = 0;
foreach ($this->lengths as $key => $value) {
$circumference += $value;
}
return($circumference);
}
}
//omitting irrelevant code
$polygon->access("write","lengths",4);
$polygon->access("write","lengths",6);
$polygon->access("write","lengths",3);
print_r($polygon->lengths);
All it prints is a 3.
@globezone Thanks, I'll try that.
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: Modifying an Object's Array Property
Posted: Thu Jun 11, 2009 7:00 pm
by Weirdan
iG9 wrote:
@Weirdan
case 'write':
if (is_array($this->{$prop})) {
$this->{$prop}[] = $value;
return(1);
} else {
$this->$prop = $value;
return(1);
}
Re: Modifying an Object's Array Property
Posted: Sun Jun 14, 2009 1:07 pm
by iG9
@Weirdan Thanks for the clarification I'll try it. Can you point me to a page explaining the curly bracket significance that makes that work? I tried searching but couldn't find anything relevant.