Result is an array

Hi, I have a small question, suppose you have a function which returns an array, the normal way of data gathering :


<?php

function blahfunction( $string )
{
     // Do something here

     return $result;
}

$handle = blahfunction('blah');
echo $handle['data'];

?>

I wanna combine two last lines in one, could it possible ? for example something like blah(‘blah’){‘data’} , does the function get any index ?

thanks.

You mean something like this?

echo blahfunction('blah')['data']; 

Did you try?

yes but returns error : $PostName = $Blog->GetPostNameByID( $ID )[‘post-name’];

What error?
And are you sure that GetPostNameByID( $ID ) returns an array? Sounds to me like a method that returns a name of a post, not an array.

yes it returns array ,

	public function GetPostNameByID( $id )
	{
		if( $this-&gt;ItemExists( $id , 'posts' , 'id' ) === TRUE )
		return $this-&gt;GetRecord("select * from `posts` where `id` = '".$id."'" , 'ASSOC');	
	}


$Blog-&gt;GetPostNameByID('4'); // return array( 'post-name' =&gt; 'something')


Ok, so what is the error you’re getting?

Parse error: parse error in F:\wamp\www\—\blog\html\comments.php on line 9

Line9 = $PostName = $Blog->GetPostNameByID( $ID )[‘post-name’];


<?php

function blahfunction( $string )
{
     $result = [
      'data' => $string,
      'data2' => $string,
      'data3' => $string,
      'data4' => $string
     ];

     return $result;
}

//$handle = blahfunction('blah');
echo call_user_func(blahfunction, 'blah')['data'];

?>

Though what you’re looking for is method chaining, i.e. methods that return objects.

and in OOP programming ?


call_user_func($Blog->GetPostNameByID, 'blah')['data']; 

Though, again, what you’re looking for is method chaining, i.e. methods that return objects.

but it still has error :frowning:
Parse error: parse error in F:\wamp\www\—\blog\html\comments.php on line 9

$PostName = call_user_func($Blog-&gt;GetPostNameByID, $ID)['post-name'];

It should work: http://www.sitepoint.com/forums/showthread.php?855985-obj()-gt-var-syntax-for-accessors&p=5129168&viewfull=1#post5129168

Need more info on what you’re having and what you’re doing.

<hr>


print_r( $Blog->GetPostNameByID( $ID ) );

gives you what?

Off Topic:

OOP programming = Object Oriented Programming programming