tags:

views:

91

answers:

5

I dont understand the function of these characters -> in this code:

$var->getImageInfo();



the function "getImageInfo()" populates the variable "$var".

I can use the print_r function to display all values but how do I get a specific value


echo "<pre>";
print_r($var->getImageInfo());
echo "</pre>";

returns

Array
(
    [resolutionUnit] => 0
    [fileName] => 1.jpg
    [fileSize] => 30368 bytes
    ...
)

how do I get "fileSize" for instance?

+2  A: 

the function "getImageInfo()" populates the variable "$var".

No, actually it calls the method getImageInfo() on the object $var.

In order to use the returned array, do this:

$res = $var->getImageInfo();
print $res['fileName'];

Read more about working with objects in PHP in the documentation.

Max
@Max: Thank you very much!
dany
+2  A: 

$var is an object (class), and getImageInfo is a function in that class that returns an array. Save the resulting array into another variable to read its contents.

$array = $var->getImageInfo();
echo $array['fileSize'];
Rocket
@Rocket: Thank you!
dany
You're welcome.
Rocket
+4  A: 

$var is an object.

getImageInfo() is one method of this object - this method returns an array.

if you want to get a specific info:

$info = $var->getImageInfo();
$fileName = $info['fileName'];
oezi
You cannot do `$var->getImageInfo()['fileName'];` in PHP.
Rocket
But you can do {$var->getImageInfo}['fileName']
Kevin Peno
@Kevin, no you can't.
Rocket
@Rocket: thanks for the hint, i removed that
oezi
@oezi, no problem.
Rocket
@oezi: Thank you!
dany
@Rocket, I'm sorry, you are right, because {} converts values to a string. `${$var->getImageInfo()}` returns an Array to String conversion. However, if anything returns a string, you may use it as such `${$var->strRtn()}`. You can also access normally unaccessible object properties similarly, e.g., `$obj->{"illegal name"}['filename']` or `$obj->{0}['filename']`. Not that this is good practice :P
Kevin Peno
+2  A: 

You are making a call to a function inside a class with this:

$var->getImageInfo()

To get it into a regular variable to access specific keys, you just need to assign it to a normal variable a la:

$this = $var->getImageInfo();
echo $this['FileSize'];
Ben Dauphinee
@Ben Dauphinee: Thank you!
dany
+3  A: 

In your example, $var->getImageInfo(), the variable $var is an instance (also called an object) of a class. The function getImageInfo() is known as a class method. This is part of Object Oriented Programming, also called OOP. You can learn more about this here - http://php.net/manual/en/language.oop5.php

If you want to get a particular member of the array that you listed, you can simply do:

$image_info = $var->getImageInfo();
echo $image_info['fileSize'];
Charles Hooper
@Charles Hooper:Thank you!
dany