Properties as an array

I want to do something like this without using additional variables:

class className { public static function func(){ return array('true','val2'); } } if(className::func()[0]) { echo 'doğru'; } else { echo 'Yanlış'; } 
+4
source share
4 answers

className::func()[0] is called array dereferencing and is not a valid syntax in all versions of PHP. It will be available starting in PHP 5.4, currently in beta , released in March 2012. For an earlier version of PHP you will need to use an additional variable somewhere to store the array returned from className::func() .

For more information on implementation details, see the PHP 5.4 Array documentation .

+11
source

Array Deferencing is currently not available in PHP. It is on the table for PHP 5.4 .

Until then, you will need an additional variable:

 $arr = className::func(); if($arr[0]){ echo 'doğru'; }else{ echo 'Yanlış'; } 
+7
source

Well, you can return the object to your method. sort of:

 class className{ public static function func(){ return (object)array('true'=>'true','val2'=>'val2'); } } echo className::func()->true;//Extra variables go away =) 
+2
source

As others have noted, you currently cannot do this. If you really cannot use a temporary variable (although I see no reason not to use it), you can use

 if(current(className::func())) // or next() or reset() 

But make sure you read the documentation for these functions to handle empty arrays correctly.

Link : current

+1
source

Source: https://habr.com/ru/post/1391672/


All Articles