Modifying an Object's Array Property

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Modifying an Object's Array Property

Post 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.
Last edited by Benjamin on Thu Jun 11, 2009 7:24 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Modifying an Object's Array Property

Post 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;
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Modifying an Object's Array Property

Post 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".
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Modifying an Object's Array Property

Post 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.
Last edited by Benjamin on Thu Jun 11, 2009 7:24 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Modifying an Object's Array Property

Post 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.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Modifying an Object's Array Property

Post 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

Code: Select all

$this->$prop[] = $value;
Trying to debug but I'm amazed this is so difficult.
Last edited by Benjamin on Thu Jun 11, 2009 7:24 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Modifying an Object's Array Property

Post by Weirdan »

This should work

Code: Select all

 
$this->{$prop}[] = $value;
 
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Modifying an Object's Array Property

Post 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.
Last edited by Benjamin on Thu Jun 11, 2009 7:25 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Modifying an Object's Array Property

Post 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.
globezone
Forum Newbie
Posts: 7
Joined: Wed Jun 10, 2009 4:57 am

Re: Modifying an Object's Array Property

Post by globezone »

try this...

Code: Select all

 
  case 'write':
              //
             $tmp =& $this->$prop;
              //
             if(is_array($tmp))
                  $tmp[] = $value;
               else 
                 $tmp = $value;
               return(1);
     break;
 
Last edited by Benjamin on Thu Jun 11, 2009 7:25 pm, edited 1 time in total.
Reason: Changed code type from text to php.
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Modifying an Object's Array Property

Post 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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Modifying an Object's Array Property

Post by Weirdan »

iG9 wrote: @Weirdan
                case 'write':
                    if (is_array($this->{$prop})) {
                        $this->{$prop}[] = $value;
                        return(1);
                    } else {
                        $this->$prop = $value;
                        return(1);
                    }
iG9
Forum Commoner
Posts: 38
Joined: Fri Jul 18, 2008 2:11 pm

Re: Modifying an Object's Array Property

Post 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.
Post Reply