Professional Web Applications Themes

Accessing properties via vars - PHP Development

I am having some problems accessing object properties through the use of a variable. I am using overloading in php 5.0.4 to create an array holding the property values. test snippet (un tested) ------------------------------------------ class example { private $itemArray; function __set( $property, $value ) { $itemArray[$property, $value]; } function __get( $property ) { return( $itemArray[$property] ); } function test( $testArray ) { $property = "Name"; $this->$property = $testArray[$property]; } } $data = array( "Name"=>"Me" ); $obj = new example; $obj->test( $data ); echo $obj->Name; ------------------------------------------ Now normally I would expect property 'Name' to be set to 'Me', and whilst the ...

  1. #1

    Default Accessing properties via vars

    I am having some problems accessing object properties through the use of a
    variable. I am using overloading in php 5.0.4 to create an array holding
    the property values.

    test snippet (un tested)

    ------------------------------------------
    class example
    {
    private $itemArray;

    function __set( $property, $value )
    {
    $itemArray[$property, $value];
    }

    function __get( $property )
    {
    return( $itemArray[$property] );
    }

    function test( $testArray )
    {
    $property = "Name";
    $this->$property = $testArray[$property];
    }
    }

    $data = array( "Name"=>"Me" );
    $obj = new example;
    $obj->test( $data );
    echo $obj->Name;
    ------------------------------------------

    Now normally I would expect property 'Name' to be set to 'Me', and whilst
    the script is executing inside the __set method it does. However as soon
    as __set returns back to the 'test' method the name element of $itemArray
    is zero'd off :( It's like the scope of the value is being lost...

    Am I doing something wrong here? or is what I am doing completely not
    allowed?

    Thanks for any help
    Dean Guest

  2. #2

    Default Re: Accessing properties via vars

    Dean L wrote: 
    [...]

    Curious, you are posting a question about an untested snippet...
     

    Use $this->itemArray instead of $itemArray inside the methods, as in:

    function __set($property, $value) {
    $this->itemArray[$property] = $value;
    }

    Tested snippet:

    class example {
    private $itemArray;

    function __set($property, $value) {
    $this->itemArray[$property] = $value;
    }

    function __get($property) {
    return $this->itemArray[$property];
    }

    function test($array) {
    foreach ($array as $k => $v) {
    $this->$k = $v;
    }
    }
    }

    Consider a more descriptive name for the test function, like setArray or
    define a __call method instead.


    JW



    Janwillem Guest

  3. #3

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in
    news:430795cb$0$67724$euronet.nl:
     
    > [...]
    >
    > Curious, you are posting a question about an untested snippet...

    >
    > Use $this->itemArray instead of $itemArray inside the methods, as in:
    >
    > function __set($property, $value) {
    > $this->itemArray[$property] = $value;
    > }
    >
    > Tested snippet:
    >
    > class example {
    > private $itemArray;
    >
    > function __set($property, $value) {
    > $this->itemArray[$property] = $value;
    > }
    >
    > function __get($property) {
    > return $this->itemArray[$property];
    > }
    >
    > function test($array) {
    > foreach ($array as $k => $v) {
    > $this->$k = $v;
    > }
    > }
    > }
    >
    > Consider a more descriptive name for the test function, like setArray
    > or define a __call method instead.
    >
    >
    > JW
    >
    >
    >[/ref]

    Darn I'm sorry, I spent all day trying to work the problem out and
    stuffed up when posting it :(

    Anyway thank you for your time, and because of my stoopidness I don't
    think the problem highlighted itself.

    So I wrote this [tested] :) To try and highlight my problem. This is
    extracted from the actual class causing the problem (took a while)

    ---------------------------------------------------------------

    class test
    {
    private $itemArray;
    private $writeProperties;
    private $updatedFields;

    function __construct()
    {
    $this->writeProperties = array( "Name", "Age" );
    $this->updatedFields = array();
    }

    function __set( $property, $value )
    {
    $this->itemArray[$property] = $value;
    $this->updatedFields[] = $property;
    }

    function __get( $property )
    {
    return $this->itemArray[$property];
    }

    public function setArray( $array )
    {
    foreach( $this->writeProperties as $property )
    {
    if( isset( $array[$property] ) && $array[$property] !=
    "" )
    {
    $this->$property = $array[$property];
    }
    }
    }

    public function getUpdatedFields()
    {
    return $this->updatedFields;
    }
    }

    $obj = new test();
    $dataArray = array( "Name"=>"Me", "Age"=>"30" );
    $obj->setArray( $dataArray );

    echo "Fields updated -> <pre>";
    print_r ($obj->getUpdatedFields() );
    echo "<pre>";

    ---------------------------------------------------------------

    If you look at the property $updatedFields it should end up populated
    with two elements, "Name" & "Age". However they are populated with zero
    values.

    The curious thing is that inside the __set function the property has the
    correct values in it, as soon as it moves outside the method they are
    zero.....Can't see why it is doing this (might be code blind by now
    though :( )

    Apologise again for the bad initial post...

    thanks
    Flibble Guest

  4. #4

    Default Re: Accessing properties via vars

    Flibble wrote: 

    On my machine, a print_r on $updateFields produces:

    Array
    (
    [0] => Name
    [1] => Age
    )

    and a print_r on $itemArray produces:

    Array
    (
    [Name] => Me
    [Age] => 30
    )

    So your class appears to work as it should.


    JW



    Janwillem Guest

  5. #5

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in news:4307b00b$0$6827
    $euronet.nl:
     
    >
    > On my machine, a print_r on $updateFields produces:
    >
    > Array
    > (
    > [0] => Name
    > [1] => Age
    > )
    >
    > and a print_r on $itemArray produces:
    >
    > Array
    > (
    > [Name] => Me
    > [Age] => 30
    > )
    >
    > So your class appears to work as it should.
    >
    >
    > JW
    >
    >
    >
    >[/ref]

    Hmm. curious

    The strange thing I have here is that I added the following after the
    class to display the output (I also added a getItemArray() method to
    return the data array).

    echo "fields Updated -><pre>";
    print_r( $obj->getUpdatedFields() );
    echo "</pre>data -> <pre>";
    print_r( $obj->getItemArray() );
    echo "</pre>

    If I run it in a normal browser it just outputs a single zero....If
    however I step through debug (Using Zend Studio) it displayes the
    output...but the $updateFields are still blank, whilst the $itemArray is
    populated correctly...


    I starte changing lines around and the offending item seems to be in the
    setArray() method

    $this->$property = $array[$property];

    If I change this (for test purposes) to:

    $this->testItem = $array[$property];

    Everything works perfectly as it should...As soon as I put a variable in
    there its almost like it halts the script from running.

    May I ask what OS your server is running on? Mine is Fedora Core 3..maybe
    it is OS related?
    Flibble Guest

  6. #6

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in news:4307b00b$0$6827
    $euronet.nl:
     
    >
    > On my machine, a print_r on $updateFields produces:
    >
    > Array
    > (
    > [0] => Name
    > [1] => Age
    > )
    >
    > and a print_r on $itemArray produces:
    >
    > Array
    > (
    > [Name] => Me
    > [Age] => 30
    > )
    >
    > So your class appears to work as it should.
    >
    >
    > JW
    >
    >
    >
    >[/ref]

    Also found this, sounds like it could possibly be related...alas its a
    closed bug :(

    http://bugs.php.net/bug.php?id=22237
    Flibble Guest

  7. #7

    Default Re: Accessing properties via vars

    Flibble wrote: 

    I don't see how this bug would be related. BTW, I'm using the latest 5.1
    beta release without Zend studio. Try matching up to my configuration and
    see what happens.


    JW



    Janwillem Guest

  8. #8

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in
    news:430843c4$0$80046$euronet.nl:
     
    >
    > I don't see how this bug would be related. BTW, I'm using the latest
    > 5.1 beta release without Zend studio. Try matching up to my
    > configuration and see what happens.
    >
    >
    > JW
    >
    >
    >
    >[/ref]

    Ok I'll try upgrading to 5.1 to see if it makes the diff...

    Thanks Janwillem
    Flibble Guest

  9. #9

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in
    news:430843c4$0$80046$euronet.nl:
     
    >
    > I don't see how this bug would be related. BTW, I'm using the latest
    > 5.1 beta release without Zend studio. Try matching up to my
    > configuration and see what happens.
    >
    >
    > JW
    >
    >
    >
    >[/ref]

    Well upgrading was very unsuccessful, after spending a day trying to get
    things working I had to revert back to 5.0.4. So I am unable to test this.
    I will change the class round to work diferently to avoid this problem (not
    ideal). Unfortunately even if 5.1 solved this problem I would be unable to
    use it as Zend studio isn't compatible with it, and I like its debugging
    facilities to much to not use it.

    If I had more time I would have persisted in trying to get 5.1 working
    correctly...alas time is the one thing I don't have a lot of at the moment.

    Thanks for your time


    Flibble Guest

  10. #10

    Default Re: Accessing properties via vars

    "Janwillem Borleffs" <com> wrote in
    news:430843c4$0$80046$euronet.nl:
     
    >
    > I don't see how this bug would be related. BTW, I'm using the latest
    > 5.1 beta release without Zend studio. Try matching up to my
    > configuration and see what happens.
    >
    >
    > JW
    >
    >
    >[/ref]

    Additional:

    I think I'll give it another go when theres a stable realse of 5.1
    Flibble Guest

  11. #11

    Default explode() behaviour?

    Dear group,

    I'm trying to explode a tab delimited string into
    an array of strings, using explode.

    It seems that when I explode "\t\t\tone\ttwo\tthree\tfour"
    it gives me 4 strings: "one", "two", "three", "four".

    I expected to get 6 strings: "","","one", "two", "three", "four".

    Is this normal behaviour, or is there another method
    to get what I want?

    --
    Thanks, Frank.
    (remove 'q' and '.invalid' when replying by email)



    Frank Guest

  12. #12

    Default Re: explode() behaviour?

    "Frank Bemelman" <invalid.nl> wrote in message
    news:4312ffba$0$11178$news.xs4all.nl... 


    I got exactly what you expected to get. Using PHP5 on IIS6/Win2003.

    $kaboom = explode ("\t", "\t\t\tone\ttwo\tthree\tfour");
    var_dump($kaboom);

    prints
    array(7) {
    [0]=>
    string(0) ""
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(3) "one"
    [4]=>
    string(3) "two"
    [5]=>
    string(5) "three"
    [6]=>
    string(4) "four"
    }

    --
    Welcome to Usenet! Please leave tolerance, understanding
    and intelligence at the door. They aren't welcome here.
    eternal piste erection miuku gmail piste com


    Kimmo Guest

  13. #13

    Default Re: explode() behaviour?

    "Kimmo Laine" <com> schreef in bericht
    news:%CDQe.14574$news.jippii.net... 

    Hmm, yes, something is going on here. I messed up my entire
    database now, so I need to do a bit more drilling and digging.
    Thanks anyway, explode seems to do what it is supposed to do.
    It's strange, because if I replace all '\t' with '~' it
    does work as expected.

    Renaming all my vars to kaboom1, kaboom2, etc, suddenly
    looks like a very good idea too.

    Thanks!

    Oh well... now where's my backup...

    --
    Thanks, Frank.
    (remove 'q' and '.invalid' when replying by email)


    Frank Guest

Similar Threads

  1. Accessing COM properties that take arguments
    By Jim@Abcam in forum Coldfusion - Advanced Techniques
    Replies: 0
    Last Post: July 11th, 04:27 PM
  2. Accessing IIS Logfile configuration properties
    By IIS LogFile configuration properties.... in forum ASP.NET Web Services
    Replies: 0
    Last Post: April 22nd, 02:53 PM
  3. Accessing properties in custom control
    By Omid in forum ASP.NET Building Controls
    Replies: 0
    Last Post: May 23rd, 04:33 PM
  4. accessing properties of loadMovie clips
    By Crack Wilding in forum Macromedia Flash
    Replies: 1
    Last Post: September 2nd, 07:35 AM
  5. Accessing all properties of an object at Runtime
    By Ilyas in forum ASP.NET General
    Replies: 2
    Last Post: July 7th, 07:13 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139