Jump to content

[SOLVED] Question on php pointers


thenewperson

Recommended Posts

I think these this is a pointer "->"

my question is what does it do i often see it in open sources like

 

$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());

      mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

$this -> num_members = -1;

 

so on....

 

i see $this -> used so many times in same source so bit confued

my guess is their storing bunch of values into $this

but i dont understand the advantage of that

Link to comment
https://forums.phpfreaks.com/topic/180213-solved-question-on-php-pointers/
Share on other sites

-> is used to access methods and properties of an object in PHP OOP. There are plenty of examples there to help you understand.

 

The reserved variable $this is used to access methods and properties of a class from within one of it's methods.

the $this->whatever syntax is PHP's way of accessing class variables/methods within a class. PHP doesn't have a pointer type.

 

in C++ it is similar to the this->whatever syntax, but in PHP you always must use $this->whatever when accessing attributes/methods inside a class, where in C++ you don't have to. It differs where in C++ this refers to the location in memory that the object resides (basically it is a pointer to itself) while in PHP it simply means it is referencing attributes or whatever in the class

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.