OOP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

OOP

Post by Nay »

Okay, I've been hearing about this OOP or Object Orientated Programming, if I'm not mistaken.

I've searched high and low for an article and have found many - many that just ends up confusing me. I want to go futhur into PHP as it is a very interesting language. I've been able to code my own portal admin control panel since the 3 months that I've started learning PHP.

So what exactly is OOP? Anyone?

Thanks...

-Nay
User avatar
Orkan
Forum Commoner
Posts: 32
Joined: Sun Aug 24, 2003 9:07 am
Location: Ukraine
Contact:

OOP

Post by Orkan »

Hey!

I'm not very familiar with this subject... So I can't tell you a definition of OOP. I've read bout 100 pages (books, articles, etc...) bout subj. There were a lot of definitions, but I still couldn't understand what it's used for...

And only yesterday I did it! But still I have some hesitations... As I understood, OOP is used for grouping many functions into one object for creating complicated subProgram. But the only benefit I can get from it - is that I have no problems using global $var; in each function.

I'm also interested in this subject and would like to hear clear point on the subj...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

There are several Tutorials on the subject at the phpcomplete.com website.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

You may want to check out this intro to OOP I found on Dr. Dobbs. Some key words to look for: objects (obviously), class, abstraction, inheritance and polymorphism.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

O_O

Oh.....kay. I've just did a read though of most of the text on that site and I'm completely puzzled.

Must be my knowledge. I think I need to learn somemore before doing OOP.

From my understanding now, OOP is more of a concept. It's not something limited to PHP only. But then I could be wrong...

-Nay
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

Here is the basic idea of OOP...

OOP tries to model real world things for example...

you have a toaster, what does it do? It burns bread right? Now how does it get its power? It has a power plug you plug into a wall.

That toaster is an object, its internal parts are kept inside its plastic case(called encapsulation) It has two slots to put bread in right? It also has a power plug to plug into the wall (called the interface).

Basically the key with objects is that a user doesn't have to know how the bread gets toasted or if the power going to the toaster comes from coal, nuclear, or solar power. All the toaster cares about is when it gets plugged that it has any power.

I can take that toaster to anyone's house and I can say that as long as I have the right power supply that toaster will work. So OOP really is about creating objects that work together. You can take a user signup application and be able to use that anywhere you want as long as you tell the user what info they need to supply it(or power it).

I hope that makes things a little clearer for you. It took me a while to get the hang of it.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

Nay: People use OOP for design patterns, ease of development, etc. Like you said, not something limited to PHP in the least. More of a methodology that languages use, rather than a language itself. You can code in everything from SmallTalk to C++ to JavaScript using OOP practices.

JPlush76 really has the right idea of using analogies like that to explain OOP.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

oop is not just a concept. it's taught as one, but it's not. it's a style.


take, for example, designing a website.
there's two ways you can do it.

one: every single page is self contained. if you wanna change the color on your website you have to edit everypage

two: you have one or more includes pages with all the includes you need. these are included with EVERY page. they have functions that create elements you use everywhere... ie:

Code: Select all

function bgnpg ($title){ # [b]B[/b]e[b]G[/b]i[b]N[/b]'s each [b]P[/b]a[b]G[/b]e
  /* set cookies */
  /* set sessions */
  /* set other headers */
  echo <<<END
<html>
  <head>
  <!-- this is the header for EVERY PAGE OF THE SITE. you have this defined ONCE in ONE file. all changes will affect ALL pages -->
  <title>$title</title>
  <!-- meta info -->
  <!-- java script stuff -->
  <!-- css stuff -->
  <!-- other script stuff -->
  </head>
  <body bgcolor="#123acd" text="567bef">
END;
}
that's in your include file. in each php page you use to make the site, before you put the body, you call bgnpg('some title appropriate to the page');
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

trollll wrote:Nay: People use OOP for design patterns, ease of development, etc. Like you said, not something limited to PHP in the least. More of a methodology that languages use, rather than a language itself. You can code in everything from SmallTalk to C++ to JavaScript using OOP practices.

JPlush76 really has the right idea of using analogies like that to explain OOP.
java is meant to be oop... where each thing is done by calling a "modual" previously created for doing that.

want me to post a unix-like shell made in java to demonstrate oop?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

m3rajk's example isn't really OOP. Its an example of modular/library code, but its not OOP.

In OOP your application is structured around a collection of "things" (aka objects) that interact. You'll call methods (functions that are part of the object), you pass objects around to other objects.

OOP is one of many paradigms for organizing a code. Others are Modular/Procedural or Functional (note functional is very very very different from Procedural, look at a languge such as Haskell or Prolog or Scheme). Often you'll use elements of each even within the others. All of these use promote code-reuse, which is really the point m3rajk was getting at
Last edited by nielsene on Tue Aug 26, 2003 9:59 pm, edited 1 time in total.
jayr517
Forum Commoner
Posts: 26
Joined: Fri Aug 22, 2003 7:28 pm
Location: Boise, ID
Contact:

Post by jayr517 »

Nice posts...thought I'd throw in my 2 cents as well. The easiest way for me to describe OO is with an example. I learn by example...maybe some of you do too.

PHP mail() function is a simple way to send mail...send it a recipient, subject and body. However, what if you want to add a signature? or send a copy to the webmaster? or capture the IP? or save the email to a DB table? etc, etc, etc. All of this can obviously be done via one include script, but OO languages offer a more encapsulated way of doing this (I'm not getting into which I think is better, just clarifying the fact that OO doesn't necessarily offer added functionality...just a different approach).

Anyway...you can create an "Email" class which would provide the added functionality, and keep the ease of use that the mail() function provides. A simple class would look something like this:

Code: Select all

<?php 
require_once("./php/MyBaseObject.php"); 

class Email extends MyBaseObject &#123; 

    var $recipient, $subject, $body, $ip; 
    var $webmaster = "webmaster@mydomain.com"; 

    function Email($recipient, $subject, $body) &#123; 
        $parentClass = get_parent_class($this); 
        parent::$parentClass(); 
        $this->setRecipient($recipient); 
        $this->setSubject($subject); 
        $this->setBody($body); 
        $this->setIP($_SERVER&#1111;'REMOTE_ADDR']); 
    &#125; 
    
    function getRecipient() &#123; 
        return $this->recipient; 
    &#125; 

    function getSubject() &#123; 
        return $this->subject; 
    &#125; 

    function getBody() &#123; 
        return $this->body; 
    &#125; 

    function getIP() &#123; 
        return $this->ip; 
    &#125;    

    function setRecipient($recipient) &#123; 
        // add validation code here... 
        $this->recipient = $recipient; 
    &#125; 

    function setSubject($subject) &#123; 
        // add validation code here... 
        $this->subject = $subject; 
    &#125; 

    function setBody($body) &#123; 
        // add validation code here... 
        $this->body = $body; 
    &#125; 

    function setIP($ip) &#123; 
        // add validation code here... 
        $this->ip = $ip; 
    &#125; 

    function send()&#123; 
        mail($this->recipient, $this->subject, $this->body); 
        $this->saveEmail(); 

        $wmSubject = "EMAIL WAS JUST SENT FROM YOUR SITE"; 
        $wmBody    = "To:        $this->recipient\n 
                      Subject:   $this->subject\n 
                      IP Source: $this->ip\n 
                      Body:\n 
                      $this->body"; 

        mail($this->webmaster, $wmSubject, $wmBody); 
    &#125; 

    function saveEmail() &#123; 
        $this->getDbConnection(); 
        $this->selectDb($APPLICATION_DB); 

        $r  = $this->recipient; 
        $s  = $this->subject; 
        $b  = $this->body; 
        $ip = $this->ip; 

        $query = "INSERT into email_t 
                   values('','$r','$s','$b','$ip', NOW())"; 

        $this->runQuery($query); 
    &#125; 
&#125; 
?>
Simple enough to create and you can add attributes (i.e. a signature, or data validation) whenever you want. To send mail using the new class, it's a simple two line addition to your code (in addition to an include/require statement):

Code: Select all

require_once("./php/Email.php"); 
myEmail = new Email($recipient, $subject, $body); 
myEmail->send();
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

JPlush76 wrote:Here is the basic idea of OOP...

OOP tries to model real world things for example...

you have a toaster, what does it do? It burns bread right? Now how does it get its power? It has a power plug you plug into a wall.

That toaster is an object, its internal parts are kept inside its plastic case(called encapsulation) It has two slots to put bread in right? It also has a power plug to plug into the wall (called the interface).

Basically the key with objects is that a user doesn't have to know how the bread gets toasted or if the power going to the toaster comes from coal, nuclear, or solar power. All the toaster cares about is when it gets plugged that it has any power.

I can take that toaster to anyone's house and I can say that as long as I have the right power supply that toaster will work. So OOP really is about creating objects that work together. You can take a user signup application and be able to use that anywhere you want as long as you tell the user what info they need to supply it(or power it).

I hope that makes things a little clearer for you. It took me a while to get the hang of it.
In otherwords, it's Lego's or Lincoln Logs meets code.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Thanks for the replies. I was wondering...

What's with the

-->

in the codes? I doubt it means equal.

-Nay
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post by liljester »

if you see "->" its prolly an object

$objectname->property_or_method_name
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Nay wrote:Thanks for the replies. I was wondering...
What's with the
-->
in the codes? I doubt it means equal.
-Nay
Take this example:

Code: Select all

<?php
A = new email();
B = new email();
?>
A & B is now 'objected' using the email-class. They both know that email is their maker. So the 'A->send()' can be translated into 'Run the Email-class's function send(), using whatever information stored in A'.

jayr517's example holds some '$this->something', where $this means 'this class' and something is a variable that you are setting.
Post Reply