doddsey_65 Posted June 2, 2011 Share Posted June 2, 2011 im trying to learn more on OOP so decided to try this code: class asf { public $_template; public function __construct() { $this->_template = new newTemplate; } } class newTemplate { public function setTemplate($template) { global $asf; $asf->_template = $template; } public function getTemplate() { global $asf; return $asf->_template; } } $asf = new asf; $asf->_template->setTemplate('default'); echo $asf->_template->getTemplate(); setTemplate() works but i get the following error on getTemplate(): Call to a member function getTemplate() on a non-object can anyone explain why and let me know how to fix it? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/ Share on other sites More sharing options...
mikesta707 Posted June 2, 2011 Share Posted June 2, 2011 its because of this line in your newTemplate class $asf->_template = $template; that is essentially overwriting your asf objects data member as the string 'default'. So after you call your set template function, your asf objects data member _template is no longer a newTemplate object, but a simple string. Now how you would get around this is something i am unsure of. Your setTemplate function seems to expect a newTemplate object, rather than a string, but beyond telling you to pass a newTemplate object, since i'm not sure what function that object is supposed to fulfill, i can't really give you a definite answer. However, there are a few conceptual issues with your setup. First of all, your mutator setTemplate uses a global variable. This is bad practice, since one of the main draws of OOP is encapsulation. Encapsulation is a concept that basically says that objects are discrete things, in which the objects data only interacts with outside data through function arguments (like through a mutator), and otherwise outside data stays out, and inside data stays in, unless formally requested (like through an accessor). By using a global variable, you kind of make encapsulation pointless. Secondly, this method seems better suited to be in the asf object, as all it does is change a data member in the asf object. Currently, if you wanted to make use of this object, you have strict requirements, like using the variable name $asf. I realize this is just an example, but these kind of concepts are important. The same kind of stuff goes for getTemplate. get/setTemplate are basically an accessor/mutator for the asf class, but are inexplicably in the newTemplate class. Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223860 Share on other sites More sharing options...
doddsey_65 Posted June 2, 2011 Author Share Posted June 2, 2011 to be honest i dont even know why i set it up like that. I saw it in someone elses code and tried to recreate it. I dont see the point of calling the newTemplate class within the asf class when the functions can all e added to the asf class in the first place. All my code on my forum works fine but is a mess. Thats why i have tried to get into OOP rather than procedural. But i suppose if the code works then that is the main thing. Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223863 Share on other sites More sharing options...
mikesta707 Posted June 2, 2011 Share Posted June 2, 2011 well code working is probably the most important thing, but code making sense is also very important. Imagine writing this code that, although it works, is all over the place and not documented well. You set it up, and forget it, and it inexplicably breaks some time down the line. Its very difficult to go through nonsensical code, especially if you haven't looked at it for a while. OOP doesn't really magically make your code make more sense, but it does a great job of separating things out a bit. Like for example, if you have a forum, you could have a page object, a user object, and a thread object. If something breaks with some sort of user functionality, you will know that the problem lies in the user object, and so on. PHPfreaks has a pretty nice tutorial on OOP, you can read it here. Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223865 Share on other sites More sharing options...
ignace Posted June 2, 2011 Share Posted June 2, 2011 Your code gives serious doubts whether you actually understand OOP. The four basic ideas behind OOP are: Inheritance, Polymorphism, Abstraction, and Encapsulation. Your classes are anything but encapsulated. And globals, seriously? Stop reading tutorials and start reading books! You are using one class to modify another (newTemplate::setTemplate())? Don't you think that functionality should then be in the class that it manipulates? Look at the below example: interface Template { public function assign($var, $value); public function render($script); } class SmartyTemplate implements Template { public function assign($var, $value) { /**/ } public function render($script) { /**/ } } class ASF { private $_template = null; public function setTemplate(Template $template) { $this->_template = $template; } } I have created an interface Template so that I am not bound to any particular rendering engine (PHP, Smarty, PHPTAL, ..): $asf->setTemplate(new SmartyTemplate()); $asf->setTemplate(new PhpTalTemplate()); That's called Abstraction and using Polymorphism I can pass any class implementing Template. The variables in ASF are encapsulated, you can only modify them through setTemplate() and on ASF terms which was not the case in your example where anyone could pass anything to _template. This would work for example in your code: $asf->_template = "booboo!"; Which is not the desired functionality. Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223934 Share on other sites More sharing options...
doddsey_65 Posted June 2, 2011 Author Share Posted June 2, 2011 ive never quite grasped OOP. I have used it thus far in cases such as the user system. I would hold info on the user in the user class so i could call it like so $user->email; or i could use $user->setup($uid) which would get the info for me. I have never used complex OOP. I havent looked at many tutorials. The example i posted was "ripped" from some code from an online forum. Thanks for the advice. even though my code does work procedurally i think i will definatly need a book or two on OO php. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223937 Share on other sites More sharing options...
ignace Posted June 2, 2011 Share Posted June 2, 2011 PHP 5 Objects, Patterns, and Practices by Matt Zandstra is still the best around I think: http://www.apress.com/9781430229254 Quote Link to comment https://forums.phpfreaks.com/topic/238161-class-help/#findComment-1223941 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.