PHP Classes

OOP tutorial

Recommend this page to a friend!

      PHP Classes blog  >  Extending PHP Classes...  >  All threads  >  OOP tutorial  >  (Un) Subscribe thread alerts  
Subject:OOP tutorial
Summary:Nice tutorial
Messages:3
Author:Jim Burns
Date:2015-05-20 14:43:02
Update:2015-05-21 01:38:53
 

  1. OOP tutorial   Reply   Report abuse  
Picture of Jim Burns Jim Burns - 2015-05-20 21:45:12
Thanks for taking the time to post this Dave. Real world examples are a great aid to understanding purpose.

Can you describe a specific problem that using classes solves? For example, method A requires this and that, and creates these challenges. By using method B (classes), these challenges can be overcome in the following way.... Thanks!

  2. Re: OOP tutorial   Reply   Report abuse  
Picture of Dave Smith Dave Smith - 2015-05-20 22:39:03 - In reply to message 1 from Jim Burns
Anyone who has been around computers and programming for awhile understands procedural style programming. One of the problems with procedural style is that it is very linear and does not adapt well to code re-utilization.

So, to try and answer your question without writing another article...

Procedural programming requires a linear set of instructions to obtain a result and these instructions have to be repeated over and over to obtain results using different factors.

Object oriented programming uses inheritance to change the factors in each object, so all you have to do is get the object you want to obtain the result.

Put simply, objects are much better suited for code re-utilization than procedural programming.

Hope this example is more helpful than confusing.

Dave

  3. Re: OOP tutorial   Reply   Report abuse  
Picture of Dave Smith Dave Smith - 2015-05-21 01:38:53 - In reply to message 1 from Jim Burns
After giving it some additional thought, I think I have come up with a less abstract example...

Fact: A HTML document is nothing more than a long string of text.

Problem: How to dynamically change values in an HTML document?

Method A: Use regular expressions (regex) to parse the document, locate the matching string and then rewrite the document with the changes.

The challenge to using this method, aside from regular expressions being difficult to manage, is that regular expressions can get greedy if the document is not formatted exactly as expected. One variance, and the replacement either fails or changes the document so drastically that it becomes unusable.

Method B: Document Object Model (DOM), create an object model that represents a document and all its parts.

This allows a very elegant process to make the changes to the part of the object, document in this case, that you are after. If you have ever seen, or used...

this.document.getElementById(id).value = 'New Value'

You have been using the DOM, whether you knew it or not. This object model has changed static web pages to the feature rich, dynamic ones we see today, which would not be possible without it.

Dave