Garri

In my free time, I do programming.

OOP in PHP

Yeah! I know this blog was requested to record my learning in ASP.NET 2.0 and C#. But I'm currently doing OOP in PHP right now and I'm very happy on what I'm learning. Hopefully this helps to newbies like me enjoy OOP and practice it.

ok i've been reading the PHP Manual about OOP in PHP 5 but still I get confused on how will this work on me. I believe that OOP will help in a lot of ways. As of the moment I feel that it does. Everything you will see here are just self study, I hope you understand. Big Smile

I'm not sure either if my understanding is right or if im doing it correctly. The thing about separation of UI to the presentation logic?
Here's what i'm doing I'm avoiding embedding all PHP codes to my HTML codes. I'm just embedding the include one's.

e.g. portCategory.php
<?php
class portCategory {
   
    //Initialize Variable here.

    function portCategory () {
    }
   
    function setLink($value) {
       $this->LinkURL=$value;
    }

    function setThumb($value) {
       $this->ThumbURL=$value;
    }

    function setCaption($value) {
       $this->Caption=$value;
    }

    function displayCategory($value) {
       echo sprintf ('
                <div id="float">
                   <a href="%s"><img src="%s" />
                   <br />
                   <p>%s</a></p>
                </div>
       '
,$this->LinkURL, $this->ThumbURL, $this->Caption);
    }
?>


e.g. (Here's what I emebedded along with the HTML Code)
<?php
include ('portCategory.php'); // This contain my Class portCategory. 
include ('connectionDB.php'); //My DB Connection.
           

//Query all data needed from the database.
$myQuery = "SELECT * FROM myTable"
$resultMyQuery = mysql_query($myQuery);

//create While
while ($data = mysql_fetch_array($
resultMyQuery, MYSQL_BOTH) {
    $portCategory = new portCategory;
    $portCategory->setThumb($data['ImageURL']);
    $portCategory->setCaption($data['Caption']);
    $portcategory->setLink($data['LinkURL']);
    $portCategory->displayPortCategory();
}
?>

Posted: 10-18-2006 11:25 AM by Garri | with 3 comment(s)
Filed under:

Comments

cruizer said:

basically it's about separation of concerns. you separate your code that's concerned with the user interface and call it your presentation layer. you separate your code that's concerned with processing, computations and other operations and you call it your business layer. then you separate your code that deals with data retrieval and other operations and call it your data access layer.

that way, in case you change your user interface, you only need to change the presentation layer. if you change the way you process information or compute stuff, you only need to change the business layer. if you change the way you store or retrieve data from your data sources, you only need to change the data access layer.

# October 17, 2006 8:55 PM

lamia said:

It's great to see you improve Garri! :)

# October 17, 2006 10:55 PM

Garri said:

Ganun~ maraming thank you sa DevPinoy family! tsaka sa phpugph!

# October 18, 2006 2:52 AM