Jump to content

The use of "->" ?


kericr

Recommended Posts

I'm new to the site and to PHP. I recently lost my job as a ColdFusion developer and found that CF work in the area that I live is extremely rare, and have decided that picking up PHP can be of some benefit in helping me find new work.

 

I'm going through several different references, tutorials, and lessons and writing several testing-scripts in order to try to learn the language as quickly as I can. I figure right now, I've got about a 50% grasp on the neiuances of PHP.

 

I've run into something that I haven't seen yet and can't seem to find a quick explination as to what it is, and that is the use of '->' in PHP code. Pulling an example from a user-contributed post on php.net demonstrating the use of an exception:

 

try {
   echo inverse(5) . "\n";
   echo inverse(0) . "\n";
} catch (Exception $e) {
   echo 'Caught exception: ',  $e->getMessage(), "\n";
}

 

I've seen this symbol used elsewhere not related to exceptions, but it seems that it's required when throwing a custom exception. Can anyone explain to me (or send me to a link that does) what exactly this is and what it does?

Edited by kericr
Link to comment
Share on other sites

The -> symbol is used to access fields and methods in object oriented PHP. Consider the following code (you might want to download it and try it yourself):

 

<?php
class Dog{
   private $name = '';

   public function __construct($name)
   {
       $this->name=$name;
   }

   public function getName(){
       return $this->name;
   }

   public function bark(){
       return 'Woof Woof!';
   }
}

$fido = new Dog('Fido');
echo $fido->bark(); // outputs Woof Woof!

echo "My dog's name is ".$fido->getName(); // My dog's name is Fido


?>

 

By the way, PHP only gained decent OO features with version 5, which thankfully, is becoming more common now. Version 5.3 and the upcoming version 6 promise even better OO support.

 

For a good primer on OOP in PHP, check out these videos:

http://www.killerphp.com/tutorials/object-oriented-php/

 

Hope that makes sense. Good luck with job-hunting, and please ask if you have any more questions.

Edited by monkeysaurus
Link to comment
Share on other sites

No problem. Looking at your original example, a couple of things come to mind.

 

Try / catch loops, although useful, are pretty slow in PHP. Although they're a really useful feature, it's much more common in the real world (in my experience) to use conditional (if/else) logic to catch errors.

 

Also, in your example, '$e' would be an instance of the error object, which has the method getMessage(). A little bit like how $fido is an instance of the dog object, and has the method bark().

Link to comment
Share on other sites

Simply that the OO way of dealing with exceptions only came in at version 5; I still don't consider it safe to use that method because my code may be used on servers where PHP5 isn't available. Also, performance wise, try/catch isn't particularly quick.

 

Full tutorial on the subject here:

http://www.w3schools.com/php/php_exception.asp

Edited by monkeysaurus
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...