Jump to content


Photo

PHP OOP Programming


  • Please log in to reply
2 replies to this topic

#1 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 05 April 2006 - 09:31 PM

Hello all,

I have been coding php for about 2 years and never devolped any of my scripts object oriented. I would like to change this, but I am a little confused what the approach is.

I do mostly all forms and error checking with php and then storing the results of forms in mysql. Then I grab the mysql data and display it in tables which allows users to sort, search, page numbering, etc... I do use function calls, but not objects.

I currently code in C# and do OOP all the time, but it seems C# applications take use of objects more effectively because of the strucuture of the lanaguage and the type of work I am doing in C#. For example if I was to create an object rectangle.

I would do:

class rectangle
{
       int width;
       int height;      
 
       public rectangle()
       {
             width = 0;
             height = 0;
        }

       public rectangle(int w, int h)
       {
           width = w;
           height = h;
       }

       public void toString()
       {
             return "Rectangle Width= " + width + "\nRectangle Height= " + height;
       }
}

See maybe it's just because I dont know to code OOP in php, but C# OOP flows easily. Thanks for any help and advice.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 06 April 2006 - 01:37 AM

In PHP 4, your code would look something like this:

<?PHP
class rectangle
{
       var $width;
       var $height;

       function rectangle()
       {
             $this->width  = 0;
             $this->height = 0;
        }

       function setRectangle($w, $h)  // Can't have same name again
       {
           $this->width  = (int) $w;
           $this->height = (int) $h;
       }

       function toString()   // Assumed public
       {
             return "Rectangle Width= " . $this->width . "\nRectangle Height= " . $this->height;
       }
}

$small_rect = new rectangle();

$small_rect->setRectangle(20, 10);

echo $small_rect->toString();

?>
[a href=\"http://us2.php.net/manual/en/language.oop.php\" target=\"_blank\"]http://us2.php.net/manual/en/language.oop.php[/a]


In PHP 5, your code would look something like this:

<?PHP
class rectangle
{
       public $width;
       public $height;

       public function __construct()    //or can call it: rectangle() 

       {
             $this->width  = 0;
             $this->height = 0;
       }

       public function setRectangle($w, $h)  // Can't have same name again
       {
           $this->width  = (int) $w;
           $this->height = (int) $h;
       }

       public function toString()
       {
             return "Rectangle Width= " . $this->width . "\nRectangle Height= " . $this->height;
       }
}

$small_rect = new rectangle();

$small_rect->setRectangle(10, 20);

echo $small_rect->toString();

?>
[a href=\"http://us2.php.net/manual/en/language.oop5.basic.php\" target=\"_blank\"]http://us2.php.net/manual/en/language.oop5.basic.php[/a]

#3 JustinK101

JustinK101
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego, California, US

Posted 06 April 2006 - 10:06 PM

Ok, thanks for the quick overview, but I am failing to see how using classes is possible with forms and data from the forms being posted into mysql. Such as most of the work done in PHP.

Let me do an example:

AGAIN I AM GOING TO STICK WITH C# FOR NOW
class account
{
     string first_name;
     string last_name;
     string email_address;
 
    
    //BY THE WAY I NOTICED YOU USED $THIS WHEN REFERING TO CLASS MEMBER VARIABLES IN PHP, IS THAT ALWAYS NEEDED? IN C# I CAN JUST REFER TO THEM WITHOUT THIS, SO JUST WONDERING.

    //DEFAULT CONSTRUCTOR 
    public account()
    {
       first_name = "";
       last_name = "";
       email_address = "":
     }

     // OVERIDEN PARAMETER CONSTRUCTOR
     public account(string f, string l, string e)
     {
        first_name = f;
        last_name = l;
        email_address = e:
     }    

     //FIRST_NAME PROPERTY, A REALLY NICE FEATURE OF C#
    public string first_name
    {
       get { return first_name; }
       set { first_name = value; }
    }

    //LAST_NAME PROPERTY
    public string last_name
    {
       get { return last_name; }
       set { last_name = value; }
    }

    //EMAIL_ADDRESS PROPERTY
    public string email_address
    {
       get { return email_address; }
       set { email_address = value; }
    }
}

Ok so now I have my class I guess when a user fills in data into the form I just create a new object:

account ac1 = new account('Justin', 'Smith', 'js@email.com');
$_GET['first_name'] .. ETC...

But what I am trying to get across is that I don't see the advantage to using OOP for PHP applications. if I am just going to store all data into mysql and not have many instances of object account in memory whats the point?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users