Jump to content

PHP OOP Programming


JustinK101

Recommended Posts

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:

[code]
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;
       }
}
[/code]

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.
Link to comment
Share on other sites

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

[code]
<?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();

?>
[/code]
[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:

[code]
<?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();

?>
[/code]
[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]
Link to comment
Share on other sites

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
[code]
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; }
    }
}
[/code]

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.