A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Dynamic properties with PHP classes

  1. #1
    Stupid Little Dreamer
    Join Date
    Aug 2000
    Location
    Toronto
    Posts
    583

    Dynamic properties with PHP classes

    My question concerns referring to properties of php class instances dynamically. I know how to do it in Flash, so naturally I'm trying to find its counterpart in php.

    In Flash, if you have a class called Animal which contains the properties species and isHairy, then you could do something like this:
    Code:
    // EXAMPLE 1 -- FLASH
    fido = new Animal ();
    fido.species = "dog";
    fido.isHairy = true;
    Or you could access the properties dynamically:
    Code:
    // EXAMPLE 2 -- FLASH
    fido = new Animal ();
    property1 = "species";
    property2 = "isHairy";
    fido [property1] = "dog";
    fido [property2] = true;
    ... which would have the same effect.

    Now the php counterpart to the first example would be this:
    Code:
    // EXAMPLE 1 -- PHP
    $fido = new Animal ();
    $fido->species = "dog";
    $fido->isHairy = true;
    But what's the php equivalent to the second example? Is it even possible? Or maybe with some indirect method?

    Many thanks,
    Jamie
    If it weren't for the last minute, nothing would get done ...

  2. #2
    Stupid Little Dreamer
    Join Date
    Aug 2000
    Location
    Toronto
    Posts
    583
    Well, I eventually got an answer from someone else, so I'm posting it here in case it should ever be of use to anyone else:
    Code:
    <?php
    class fido
    {
        var $species;
        var $isHairy;
    
        function pet() {
            return "Give me food!";
        }
    }
    // test it out
    $d = new fido;
    $h = 'isHairy';
    $s = 'species';
    $p = 'pet';
    
    $d->$h = true;
    print "fido->isHairy: ".$d->$h."\n";
    $d->$s = 'Heinz57';
    print "fido->species: ".$d->$s."\n";
    print "fido->pet: ".$d->{$p}()."\n";
    ?>
    If it weren't for the last minute, nothing would get done ...

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center