Jump to content

problem with dereference marker/pointer


Jaswinder
Go to solution Solved by kicken,

Recommended Posts

can anyone clear me what is the difference between  ->    and   => ??

 

when we call method of a class, we use

 

$obj -> method();

 

can we use this also ???

 

$obj => method();

 

and also ,when we assign a value to a variable ( e.g $name ) , we us

 

$obj->name="Smith";

 

but what this means ??  $obj->$name ..    or is it incorrect  ??  or am i  assigning value of $name to $obj ??

 

i have seen a working code ... what it means

 

$obj= new name;  //name is a class
foreach($obj as $var=>$value)

 

 

 

 

Link to comment
Share on other sites

  • Solution

can anyone clear me what is the difference between  ->    and   => ??

-> is used by to accesses a member of an object (function or property)

=> is used by to define a key/value pair.

 

For example:

$dtobject = new DateTime('now');  //A new datetime object
$dtobject->format('m/d/y');       //Access the format method of $dtobject and execute it.


$arr = array(                     //Define a new array
   'somekey' => 'somevalue'       //Define a key/value pair to store in that array
);

can we use this also ???

$obj => method();

No, likewise you cannot use -> in place of => when dealing with arrays.

 

but what this means ??  $obj->$name ..   

It is an application of variable-variables. What happens is PHP uses the value of $name as the name of the property to access. Eg:

 

$field = 'firstname';
$obj = new stdClass;
$obj->$field = 'kicken';
var_dump($obj->firstname); //Shows 'kicken' because the value was assigned to $obj->firstname

i have seen a working code ... what it means

 

$obj= new name;  //name is a class

foreach($obj as $var=>$value)

It loops through all the public properties of $obj and assigns them as a key/value pair to $var (property name [key]) and $value (property value [value])
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.