SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    OOP Newbie question

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

    Code:
    <?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

    Code:
    $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

    Code:
    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);
    }
    Code:
    $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

  2. #2
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by afridy View Post

    So my question is , i don't understand why OOP is require since same think can be done using a simple function!
    Please explain
    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.

    2) 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.

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kennard View Post
    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.

    2) 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

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,244
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    "First make it work. Then make it better."

  5. #5
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by afridy View Post
    So my question is , i don't understand why OOP is require since same think can be done using a simple function!
    Please explain
    Hi,

    Procedural versus OOP is not how a developer writes it, rather it is 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
    voostind's opinion was correct for the day but no longer valid.

    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
    ictus==""

  6. #6
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    Thanks jeffy, its intresting too !

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.
    PHP Code:
    $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:

    PHP Code:
    $url="http://api.clickatell.com/http/sendmsg?user=xxx&password=xxx .. etc"
    But then decide to use it in a slightly different way:

    PHP Code:
    $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

  8. #8
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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.
    PHP Code:
    $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:

    PHP Code:
    $url="http://api.clickatell.com/http/sendmsg?user=xxx&password=xxx .. etc"
    But then decide to use it in a slightly different way:

    PHP Code:
    $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
    Thank you.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 php sms sending class and see how others have handled this - although you have to be aware that this could expose you both good and poor OOP practices

  10. #10
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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 php sms sending class and see how others have handled this - although you have to be aware that this could expose you both good and poor OOP practices
    Thanks a bunch buddy, took firm note of

    reusable
    extendable
    dependable, robust


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •