On Thu, Feb 28, 2008 at 2:16 PM, Nathan Nobbe <[EMAIL PROTECTED]> wrote:
> all,
>
>  as we have discussed previously, php does not have support for retrieving
>  array values on the same line in which they are returned.  i have created a
>  simple workaround, and would like to share.  first there is the class (w/
>  other features omitted for the post)
>  <?php
>  class ArrayClass {
>     private $theArray = null;
>
>     private function __construct($theArray) {
>         if(!is_array($theArray)) {
>             throw UnexpectedValueException('theArray must be an array!');
>         }
>         $this->theArray = $theArray;
>     }
>
>     public static function create($theArray) {
>         return new ArrayClass($theArray);
>     }
>
>     public function __get($name) {
>         if($this->isValidKey($name)) {
>             return $this->theArray[$name];
>         }
>     }
>
>     private function isValidKey($name) {
>         $isValidKey = false;
>         if(array_key_exists($name, $this->theArray)) {
>             $isValidKey = true;
>         }
>         return $isValidKey;
>     }
>  }
>  ?>
>
>  and then there is the example,
>
>  <?php
>  include('ArrayClass.php');
>
>  function sillyFunc() {
>     return array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
>  }
>
>  echo ArrayClass::create(sillyFunc())->a . PHP_EOL;
>  ?>
>
>  notice what would be
>
>  echo sillyFunc()['a'] . PHP_EOL;
>
>  becomes what you see above.
>
>  -nathan
>
>  ps.  sorry for all the extra newlines; im trying to work w/ the alterations
>  the list server is applying to my posts so bear w/ me :D
>

That is funny & interesting.  If you're only using text keys you can do this:

<?php
function sillyFunc() {
        return array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e'=>'some 
string');
}

function objectize($val) {
        return (object)$val;
}

var_dump( objectize(sillyFunc())->a );
var_dump( objectize(sillyFunc())->e );


--- output ---
int 1
string 'some string' (length=11)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to