s0c0 Posted April 11, 2007 Share Posted April 11, 2007 I created the following game to help me understand object oriented programming in PHP 5. I'm more interested in a review of my code, particularly the classes I built. Please critique and tell me what I am doing wrong. There are links to the code from the games page. http://www.mp3crib.com/alpha/dragonhero/ Link to comment Share on other sites More sharing options...
JustinM01 Posted April 11, 2007 Share Posted April 11, 2007 Looks good to me. The only thing I would say you should change are the members of your classes from public to private, and create methods to get and set them. It may seem like extra work, but that is how class members should be defined. If you make all the members in a class public, you might as well stick to procedural code. Interesting and cool way of teaching yourself OOP in PHP5 though. Link to comment Share on other sites More sharing options...
s0c0 Posted April 11, 2007 Author Share Posted April 11, 2007 Thanks for the quick response. Yes I started out trying to do that, but could not get it too work. I will reread this chapter. Do you have any books in mind specifically for OOP in PHP? I have been reading object oriented php and I don't think its all that great. Link to comment Share on other sites More sharing options...
JustinM01 Posted April 11, 2007 Share Posted April 11, 2007 I've been using "PHP5 and MySQL Bible" and the Zend PHP 5 Certification Study Guide. Basically what you want to do is this: class Something { private something_variable; public function getS() { return $this->something_variable; } public function putS($something) { $this->something_variable = $something; } It's not so much that it will make it better, its just better programming style as far as OOP is concerned. Link to comment Share on other sites More sharing options...
s0c0 Posted April 11, 2007 Author Share Posted April 11, 2007 and then from the user facing side when you require that class you pass the function variable that way? Like $anything = new Something(); $something = 'dude'; $anything->putS($something); Is that right, how is that better? I understand that it allows the function to determine how the variable is used, right? Maybe this practice is more applicable to "hardcore" OOP PHP scripts. Link to comment Share on other sites More sharing options...
JustinM01 Posted April 11, 2007 Share Posted April 11, 2007 Yeah, like I said its more for better programming style, though maybe someone with more programming knowledge than I could add something a little more informative. Basically (as far as I understand it), it helps keep the programmer from doing things with the code that could possibly cause errors. Like if you had a class variable that your code expected to be an array, but you set it to a string...well, if you send it to a function first you could possibly do some kind of error handling with it. If you just set the variable and than had a class method use it, you might run into problems. I don't know, really, as far as I see it, its really a matter of good programming style. Link to comment Share on other sites More sharing options...
s0c0 Posted April 11, 2007 Author Share Posted April 11, 2007 understood, thanks for your time. Link to comment Share on other sites More sharing options...
ted_chou12 Posted April 11, 2007 Share Posted April 11, 2007 This is all possibility Link to comment Share on other sites More sharing options...
Recommended Posts