OOP Newbie question

Hello,
i am really new to php oop and to get start i had made a simple sms object as follows.

<?php
class send_sms{
 
var $number; 
var $res;

	function send($number){ 
                $this->number=$number;

                //send sms
               $url="http://api.clickatell.com/http/sendmsg?user=xxx&password=xxx&api_id=33xx523&to=" . $this->number . "&text=test message";
               $result=file_get_contents($url);
		
		$this->res=$result;
	}
}

and i am calling this object as follows


$sms=new send_sms(); 
$sms->send('123456');
echo $sms->res;

now, instead of this i could have make a simple function to send this sms as follows


function send($no){
   $url=$url="http://api.clickatell.com/http/sendmsg?user=xxx&password=xxx&api_id=33xx523&to=" . $no . "&text=test message";
   return fopen($url);
}


$result=send('123456');
echo $result

So my question is , i don’t understand why OOP is require since same think can be done using a simple function!
Please explain :slight_smile:

Yes you’re right. For simple and basic functions there is probably no real advantage to using oop over procedural programming.

A class/object contains the properties and methods to manipulate those properties in one single stand-alone, completely portable piece of code.

Consider these 2 examples where imo oop is much more practical than procedural.

  1. a game requiring 2 dice.

You could have 2 instances of a single die class. The class has a property to store the current rolled number and methods to roll the die and return the current number.

  1. a database where you store information about books (title, author, isbn, publish date, edition etc etc)

You could have a single book class to store all the properties of the book and methods to set and get the properties. If you have multiple books to deal with at a time, you could then have an array of book instances and so the first books title could be accessed by $book[0]->title and the second book’s title could be accessed with $book[1]->title and so it is much easier to loop through a single array of book instances than it would be to handle variables $title1, $title2 etc etc. or separate parallel arrays for title, author, isbn etc etc.

For more discussion on oop vs procedural, there is lots on the Internet.

Thank you Kennard, i understand your examples and i am referring in google as well :slight_smile:

This post might help. http://www.webdeveloper.com/forum/showpost.php?p=1214861

[FONT=Verdana]

Hi,

[/FONT]Procedural versus OOP is not how a developer writes it, rather it is [COLOR=#444444][FONT=Arial]an approach to writing code. Procedural code is focused on ‘steps’ and writing code in an ordered way to create an application. If a developer focuses on state transformations, encapsulated abstractions, reflection, unit tests and pattern based code then an OOP approach is being used. Contrary to what many people believe procedural code can and often is written PHP classes but it is still procedural code it does not use Object Oriented Principles and sometimes procedural code is written more like object oriented code.

This is a very old post but is a really good one on Procedural versus OOP . In this post, please don’t pickup on the slam to PEAR as it has done a lot of good work over the last years and [/FONT][/COLOR]voostind’s opinion was correct for the day but no longer valid.
[COLOR=#444444][FONT=Arial]
There are some things that cannot be done or are very difficult to do in PHP using Procedural versus OOP, these include:

  • Mocking - a technique used to mock an instance while unit testing
  • Reflection - It is darn-right hard to build reflection into procedural code, however PHP has a whole object oriented reflection library. This is reverse engineering of classes, interfaces, and methods and can be used to create very dynamic applications.
  • You can’t do inheritance - you inherit the base functionality of a class and its’ methods and then you can extend them in a new class. Traits are are further way of reducing the limiting single inheritance structure of past versions of php (since 5.4).
  • Without a lot of work in procedural code enforcing an interface is hard and impractical using procedural code but is easy using OOP
  • Although iterators such as foreach exist in procedural php the Iterator set of compiled php classes are designed for OOP use.
  • Patterns are used with procedural functions although the vast majority are targeted at the OOP approach. These patterns can benefit developers as they describe and provide a use template to solve specific implementations for common design issues that exist. You can find many php patterns describes in the http://www.phpmaster.com website. Also may different resources books, web pages/blogs, wiki and other sources (a lot for Java that will work just fine in PHP) exist online.

Regards,
Steve[/FONT][/COLOR]

Thanks jeffy, its intresting too !

Good question, Afridy.

Sticking with your example, there are some other things you should recognise and attempt to address.

Your code is that it mostly only considers the “happy path”

It seems not to check:

-The number follows a valid pattern, is well formed.
-Depending on the country it might HAVE to conform to 04xxxxxxx or 07xxxxxxxxxxxx
-What happens if no number is accidentally passed to it.

It does not seem to be something you could use in a variety of projects, which:

-Use a different SMS gateway.
-Provide different credentials.

So, it is not very “re-usable” and neither is it very “smart”, you cannot think of it being a discrete, self-contained module to which you can delegate responsibility for doing a task.

I’m skimming over many other issues for the sake of brevity, but essentially jumping from thinking as you are now, to thinking about telling some piece of code to “Here is what you need, now just go away please and get on with this job, thats why I pay you.”

Your target might be to end up with userland code as simple as this.


$sms = new ClickatellTexter($number);
$sms->send($msg);

Knowing that ClickatellTexter will go away, maybe find its own credentials (which may be kept centrally elsewhere), do something predictable if either number is missing or if msg is empty and either report back or log something or just die quietly if it fails.

Then, with the same core code which ClickatellTexter relies upon, elsewhere in your universe - someone else - or maybe you in 6 months time can go, right, now I need a BulksmsTexter and then create a simple concrete class where the only difference between the concrete BulksmsTexter and ClickatellTexter is this line:


$url="http://api.clickatell.com/http/sendmsg?user=xxx&password=xxx .. etc";

But then decide to use it in a slightly different way:


$sms = new BulksmsTexter($number);
$sms->setCountry('UK');
$sms->setLogging('myfile.txt');
$sms->send($msg);

… so both classes are simply part of a small network of other tiny classes that are designed to operate with each other in a robust fashion.

That scenario is too outlandish and will never happen to me? Read this, how will you respond if Clickatell goes down or if a client wants to use another gateway? Copy and paste your existing class? Nope, that’s not the idea.

Buying a good OOP book which explains some of the basic principles of OOP should be on your horizon as soon as you get the syntax sorted out - but there are plenty of threads on here recommending books.

HTH

Thanks for the very valuable piece of information about oop,
yes now i understand that my oop code is really not reusable and i have to tweak it further with more methods :slight_smile:
Thank you.

All of this stuff, depends on your situation - it can be very seductive to go off and create some super set of classes containing code for YAGNI situations.

There is a general rule of thumb which you can apply:

If you only use sms for one client, then leave well enough alone.

If it happens for a second client (or second gateway) then grit your teeth, but just copy/paste, get on with other work …

But, if it happens a third time, then bite the bullet - then that’s the time to go back and refactor your code so it is:

reusable
extendable
dependable, robust

I dont want to put you off, you see … but as you say you are learning, then yes, this sms-ing class would make a good candidate for exploring and learning.

You could always post some code here as you go along for some immediate feedback, or search [google]php sms sending class[/google] and see how others have handled this - although you have to be aware that this could expose you both good and poor OOP practices :wink:

Thanks a bunch buddy, took firm note of

reusable
extendable
dependable, robust

:slight_smile: