JustinK101 Posted April 5, 2006 Share Posted April 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6688-php-oop-programming/ Share on other sites More sharing options...
toplay Posted April 6, 2006 Share Posted April 6, 2006 In PHP 4, your code would look something like this:[code]<?PHPclass 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]<?PHPclass 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] Quote Link to comment https://forums.phpfreaks.com/topic/6688-php-oop-programming/#findComment-24334 Share on other sites More sharing options...
JustinK101 Posted April 6, 2006 Author Share Posted April 6, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/6688-php-oop-programming/#findComment-24616 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.