PHP 8.4.24 Released!

Voting

: four plus two?
(Example: nine)

The Note You're Voting On

FredLawl
13 years ago
It is possible to use this as a way to create public attributes for a class.

<?php
class Foo {

  public function __construct ($array) {

      extract($array, EXTR_REFS);
      foreach ($array as $key => $value) {
          $this->$key = $$key;
          // Do: $this->key = $key; if $key is not a string.
      }

  }

}

$array = array(
    'valueOne'      => 'Test Value 1',
    'valueTwo'      => 'Test Value 2',
    'valueThree'    => 'Test Value 3'
);

$foo = new Foo($array);

// Works
echo $foo->valueOne; // Test Value 1
echo $foo->valueTwo; // Test Value 2

// Does not work!
echo $foo::$valueOne; // Fatal error:  Access to undeclared static property: Test::$valueOne
?>

<< Back to user notes page

To Top