jdlev Posted May 29, 2013 Share Posted May 29, 2013 (edited) I've never understood what this means: -> For instance, I'll see it used, and I think it typically references functions (maybe?) for instance $this->my_variable I know it's also used alot in perl. Does it provide the same function in perl? Can anyone explain this to me in layman's terms? I guess I also need to under what "$this" does? The php manual defines it as a "pseudo-variable", and I have no idea what that means? Edited May 29, 2013 by jdlev Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 29, 2013 Share Posted May 29, 2013 $this and -> are parts of PHP's object oriented syntax. Unless you're learning OOP, you can safely ignore them. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 29, 2013 Share Posted May 29, 2013 unless you want to use something like PDO...then you'll need to get used to seeing -> about the place. To expand only a little bit on what KevinM1 has stated, the use of $this and -> are tools that only apply in PHP when you are working with objects. $this is called a psudo-variable as it is used to refference the object that it is within, and does not contain a value in and of it's self - it is dependant on the object that it is inside to five the value. -> is a lot like what it looks like, it lets you drill into an object to call non-static(and perhaps static?) methods (what you casll a functon that is within an object) and parameters (variables that are at the top level of the object). so for example: //declare the object by declaring a new class: class thisIsMyObject{ //make some parameters... public $a_parameter; public $another_parateter; //make a method... public funtion setParameter($val1, $val2){ $this->a_parameter = $val1; $this->another_parameter = $val2; } } //create the object : $myObj = new thisIsMyObject(); //assign values to the parameters using the method inside the object: $myObj->setParameters("first value", "second value"); //access the values: echo "Parameter 1 = {$myObj->a_parameter} and Parameter 2 = ".$myObj->another_parameter; //change the parameter directly: $myObj->a_parameter = "I changed this directly!"; $this doesn't seem to do much here, except maybe make things a little quicker and shorter. However, it does become really rather powerfull when you take it up a gear and use inheritnace, so even if you are just starting to touch the surface of PHP OOP you really do want to be getting into the habbit of using it. Quote Link to comment Share on other sites More sharing options...
Irate Posted May 29, 2013 Share Posted May 29, 2013 Object-orientated programming is a feature of PHP since PHP 3, but it got upgraded in PHP 5 to be an actual key feature of PHP and as such, the PHP parser now treats objects differently than it did with PHP 3. As mentioned before, unless you want to write programs with objects, you will most likely never use it. You'll stumble across the mysqli class with XAMPP, so I do recommend learning about objects. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.