kericr Posted January 23, 2009 Report Share Posted January 23, 2009 (edited) 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 January 23, 2009 by kericr Quote Link to comment Share on other sites More sharing options...
monkeysaurus Posted January 23, 2009 Report Share Posted January 23, 2009 (edited) 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 January 23, 2009 by monkeysaurus Quote Link to comment Share on other sites More sharing options...
kericr Posted January 23, 2009 Author Report Share Posted January 23, 2009 Thanks a lot for the explination. I found this site through a google search that returned those videos on the link you provided; I haven't started going into them yet but they're on my task list. I appreciate the explination, that makes a lot of sense. Quote Link to comment Share on other sites More sharing options...
monkeysaurus Posted January 23, 2009 Report Share Posted January 23, 2009 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(). Quote Link to comment Share on other sites More sharing options...
kericr Posted January 23, 2009 Author Report Share Posted January 23, 2009 I'm not completely sure what you mean by using if/else instead of try/catch; I've seen if/else used in standard error-handling but not for exception-handling. Quote Link to comment Share on other sites More sharing options...
monkeysaurus Posted January 23, 2009 Report Share Posted January 23, 2009 (edited) 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 January 23, 2009 by monkeysaurus Quote Link to comment Share on other sites More sharing options...
kericr Posted January 24, 2009 Author Report Share Posted January 24, 2009 Ok, I understand. Thanks again, you've been a tremendous help to me today. 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.
Note: Your post will require moderator approval before it will be visible.