Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Which one to use and why in OOP ?

    I am wondering to know which one to use and why in OOP
    scenario:
    suppose i had a class named category.class.php
    1st method:
    php:
    PHP Code:
    <?php
    class category
    {
    // without creating properties
    function insertNew($catName,$catDesc,....)
    {
    //insert into database goes here...
    }
    }
    ?>
    2nd method
    php:
    PHP Code:
    <?php
    class category
    {
    // with creating properties
    var $catName;
    var 
    $catDesc;
    var ....;
    function 
    insertNew()
    {
    //insert into database goes here...
    }
    }
    ?>
    3rd Method:
    php:
    PHP Code:
    <?php
    class category
    {
    var 
    $catName;
    var 
    $catDesc;
    var ....;
    //creating constructor
    function category($catName,$catDesc,..)
    {
    $this->catName $catName;
    $this->catDesc $catDesc;
    .......
    }
    function 
    insertNew()
    {
    //insert into database goes here...
    }
    }
    ?>
    and i used this class in the category.php as
    1st method:
    php:
    PHP Code:
    <?php
    include "category.class.php";
    //get the parameters from the user inputs
    //creating object
    $catOBJ = new category();
    $catOBJ->insertNew($catName,$catDesc,....);
    ?>
    2nd method
    php:
    PHP Code:
    <?php
    include "category.class.php";
    //get the parameters from the user inputs
    //without creating obj
    category::insertNew($catName,$catDesc,....);
    ?>
    3rd method
    php:
    PHP Code:
    <?php
    include "category.class.php";
    //get the parameters from the user inputs
    $catOBJ = new category($catName,$catDesc,....);
    $catOBJ->insertNew();
    ?>
    Above i mentioned the ways of writing classes and implementing it, whic is the best
    method of applying the OOP method for sake of clarity..
    Thanks in advance to all of you !!!!!!!!!!!!!!!!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This depends 100% on your usage. With php 4... its not as important (still important, but not as important :P), but with php 5 it has a much stronger OOP engine.
    Your attributes or variables are what the object is using.
    Your function is your behaviour.
    Now, I don't see full code, so I know this isn't a safe assumption to make. But to me it looks like you are treating this class as a static member, which means your second calling method is the only one that will work.
    The purpose of the object is to... well... create an object. A single instance of an entity that you can manipulate as you see fit. If this was a class to say... connect to a database, you would probably create a new instance of the object (new Object()). Since it appears that you can use the entire functionality without creating an object (that is, statically), I don't really see why you would need to create a new instace of the object.
    Last edited by Fou-Lu; 02-25-2007 at 03:20 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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