Jump to content

PHP Classes


maxudaskin

Recommended Posts

Classes are great for when you want to make multiple objects. The idea is that you can run the same functions on different objects without changing any input parameters.

 

Say you have a game website where a user it taking care a multiple pets. If each pet is its own object, when a user feeds pet one, all you have to do is pet1.feed() instead of feed(pet1). The need for this is very low in common web applications compared to many other types, but it is definately a luxury to have.

 

It really depends what you are doing but I wouldn't say there are drawbacks, necesarily.

Link to comment
Share on other sites

The point of a class is to:

- encapsulate a set of data and the methods that will operate on it

- enforce strict controls on data

- support easy re-use and extension of existing code

 

There are lots of ways to use objects, the main one is as follows, creating your own data type that is used by several objects, each of which is an instance of the class:

 

class SillyString {
  private $str;

  function __construct ($value) {
    $this->str = $value;
  }


  function show () {
    echo '"', $this->str, "\"<br>\n";
  }
}

 

In PHP 5 there are few drawbacks (there is a very slight perfomance loss, but that's no drama). PHP 4 had a terrible object model.

Link to comment
Share on other sites

It is easier to implement change.  It also can present a more clear and organized way of thinking/programming from what I've seen (and done,limited).  The question for 'us' that do not yet fully grasp it is, where can we learn and switch over from procedural to OOP.  "Read this book" isn't always useful unfortunately.

Link to comment
Share on other sites

You can only learn the benefits of OOP by writing OOP and writing lots of it.

 

It also helps to work in steps.

 

1)  Stop writing procedural code.  Just write everything as classes even if it seems like more work with no benefit.  This will get you familiar with the syntax and "structural" components of OOP.  Write a small project in this way.  It doesn't have to be a web site.  It can be a command line script that runs a simulation or anything else.  But write something.

 

2)  Read about inheritance.  Think about how you could have used inheritance in your project from step #1 to decrease the amount of work you performed.  Rewrite the project from step #1 to take advantage of this.

 

3)  Introduce a new object or component into your project.  For example if your program calculates miles per gallon for cars, introduce buses, planes, boats, etc.  Write them as separate classes.

 

4)  Read about polymorphism.  Think about how you could have applied that in your project to decrease the amount of work and rewrite your project again.

 

5)  Keep writing OOP code.  Start using it everywhere.  Eventually you will start to see some re-emerging problems and road-blocks.  At this point pick up a book on design patterns and the light bulbs should start really firing off.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.