Jump to content

beta test this simple game - review code - no signup required


Recommended Posts

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/

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.

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.

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.

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.

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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.