Xdega Posted March 29, 2011 Share Posted March 29, 2011 So, I have just started learning OO PhP, and I am currently playing around with one class in particular that I had working as a function. In other words I am trying to turn this function in to a usable class. I changed my navigation function to a class and it converted flawlessly (yay), the "scripture class" is not being so nice. It returns nothing, no parse errors or anything, here is the code: <?php class kd_scripture { public function __contruct() { try { function fetchXML($url) { $ch = curl_init(); $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_TIMEOUT ,3); //GET DATA $data = curl_exec($ch); curl_close($ch); return $data; } define("USE_CURL", true); //XML STREAM URL--------------------------------------------------------------------------------------------- $url = "http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV"; //-------------------------------------------------------------------------------------------------------------------- //GET XML INFO $data = fetchXML($url); //CREATE SIMPLEXML OBJECT $char_xml = new SimpleXmlElement($data); $scripture_a=$char_xml->channel->item->description; $scripture_b=$char_xml->channel->item->title; echo $scripture_a . '<br><b>'. $scripture_b. '</b>'; } //CATCH EXCEPTION catch (Exception $e) { echo 'Scripture Module Offline!: ', $e->getMessage(), "\n"; } } } How do I fix this? Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/ Share on other sites More sharing options...
KevinM1 Posted March 29, 2011 Share Posted March 29, 2011 Functions don't map to classes in a 1-to-1 manner. This is especially true with constructors, which are special and are supposed to be used in a particular way. Slapping functions in a class in the way that you are IS NOT OOP, and really isn't teaching you anything beyond some basic syntax. You're not gaining anything from a true methodology standpoint. You'd be much better off buying a good book on the subject and learning it the right way from the start. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193517 Share on other sites More sharing options...
Xdega Posted March 29, 2011 Author Share Posted March 29, 2011 ok, thanks for the response. I do have a nice book on OO Php: http://www.amazon.com/PHP-5-Objects-Patterns-Practice/dp/1590593804 .Although the author seems to beat around the bush a lot, I am through the first few chapters with somewhat understanding. I understand writing new code from scratch is a great idea, but..... I have been working on this website for a short while, and have a bunch done already. So basically I was just trying to convert my current work that was previously a few functions inside an include. What you are basically saying is that the entire script isn't even close? All this script is meant to do is grab an xml feed and print it on the page. Since OO PHP is very diverse, what would I need to research in specific to be able to convert the functionality in my current code, in to a class? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193529 Share on other sites More sharing options...
trq Posted March 29, 2011 Share Posted March 29, 2011 Since OO PHP is very diverse, what would I need to research in specific to be able to convert the functionality in my current code, in to a class? Your missing the point. Classes do not replace functions. You cannot (or should not) simply convert a function to a class. Also, just because your code is within classes doesn't mean it is now OOP. Keep reading the book you have, it is probably the best PHP OOP book getting around, it may however take a few reads before you get it. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193533 Share on other sites More sharing options...
Xdega Posted March 29, 2011 Author Share Posted March 29, 2011 Keep reading the book you have, it is probably the best PHP OOP book getting around, it may however take a few reads before you get it. Yeah, I have re-read parts of it (and it makes more sense each time). So I guess I shall revert back to my previous method of an include file with a few functions. I can always update the website at a later date once I know OOP. thanks for clearing that up. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193540 Share on other sites More sharing options...
ignace Posted March 29, 2011 Share Posted March 29, 2011 Here's an example of your class in correct OO: class Scripture { const USE_CURL = TRUE; private $_userAgent = ''; private $_url = ''; public function __construct($url, $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1') { $this->_url = $url; $this->_userAgent = $userAgent; } public function getScripture($format = '%s<br><b>%s</b>') { $xml = $this->getXML(); $script1 = $xml->channel->item->description; $script2 = $xml->channel->item->title; return sprintf($format, $script1, $script2); } private function getXML() { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, $this->_userAgent); curl_setopt($ch, CURLOPT_TIMEOUT ,3); $data = curl_exec($ch); curl_close($ch); return new SimpleXMLElement($data); } } You could further re-factor this code and remove cURL so that the technique used can vary, file_get_contents() with stream_context_get_default() instead of cURL (Factory pattern). Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193600 Share on other sites More sharing options...
Xdega Posted March 29, 2011 Author Share Posted March 29, 2011 I see, so basically it is just taking the function and "factoring" it. Kinda sounds like math class but with code Also, the url will be specified when creating the object? eg: <?php $obj= new scripture(http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV) ?> I will definitely look in to the methods that discard the need for cURL (as much as I like it), because always looking for ways to simplify my code. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193703 Share on other sites More sharing options...
ignace Posted March 29, 2011 Share Posted March 29, 2011 Math is the foundation of a computer and thus programming. OO has laws (or principles, if you like) just like Math does. Understanding it's laws helps you to apply it correctly. Robert C. Martin identified the core principles as S.O.L.I.D., and by another author(anyone?) Tell, Don't Ask (Command-Query Separation helps you to achieve Tell, Don't Ask), Writing code is an iterative process due to changing requirements or a better understanding of the domain. For this reason you'll find a lot of people talking about re-factoring, a good book on the subject is the one by Martin Fowler or Pro PHP Refactoring. Separating your code into discrete functions helps you to make your software more flexible and thus easier to change. I could for example change the implementation of getXML() to inject the dependency instead of hard-coding cURL, and use a Factory to create the XML object. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193783 Share on other sites More sharing options...
Xdega Posted March 29, 2011 Author Share Posted March 29, 2011 Math is the foundation of a computer and thus programming. OO has laws (or principles, if you like) just like Math does. Understanding it's laws helps you to apply it correctly. Robert C. Martin identified the core principles as S.O.L.I.D., and by another author(anyone?) Tell, Don't Ask (Command-Query Separation helps you to achieve Tell, Don't Ask), Writing code is an iterative process due to changing requirements or a better understanding of the domain. For this reason you'll find a lot of people talking about re-factoring, a good book on the subject is the one by Martin Fowler or Pro PHP Refactoring. Separating your code into discrete functions helps you to make your software more flexible and thus easier to change. I could for example change the implementation of getXML() to inject the dependency instead of hard-coding cURL, and use a Factory to create the XML object. that sounds really interesting. I am pretty excited to be starting my computer science degree this fall which I know covers a lot of these concepts along with the math. I will definitely look at that php refactoring book. Although I am unsure that my syntactical command is quite there yet for anything with the word "pro" in it. Definitely something interesting to look at I guess. Maybe once I finish my current OOP book. Thanks for the insight, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/232006-class-not-working/#findComment-1193848 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.