2

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?

Zebra
  • 3,858
  • 9
  • 39
  • 52
  • 1
    *(related)* [Reference - What does this symbol mean in PHP](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Gordon Oct 28 '10 at 13:47

5 Answers5

4

$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
  • 51,017
  • 10
  • 98
  • 115
  • 2
    You cannot do `$var->getImageInfo()['fileName'];` in PHP. – gen_Eric Oct 28 '10 at 13:40
  • But you can do {$var->getImageInfo}['fileName'] – Kevin Peno Oct 28 '10 at 13:52
  • 1
    @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 Oct 28 '10 at 19:55
3

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
  • 2,739
  • 1
  • 19
  • 16
2

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
  • 15,693
  • 14
  • 81
  • 131
2

$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'];
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
2

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
  • 4,061
  • 8
  • 40
  • 59