Page 1 of 2
OOP
Posted: Mon Aug 25, 2003 4:33 am
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
OOP
Posted: Mon Aug 25, 2003 4:52 am
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...
Posted: Mon Aug 25, 2003 7:48 am
by nielsene
There are several Tutorials on the subject at the phpcomplete.com website.
Posted: Mon Aug 25, 2003 8:48 am
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.
Posted: Mon Aug 25, 2003 10:02 am
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
Posted: Mon Aug 25, 2003 10:40 am
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.
Posted: Mon Aug 25, 2003 1:28 pm
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.
Posted: Tue Aug 26, 2003 11:00 am
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');
Posted: Tue Aug 26, 2003 11:02 am
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?
Posted: Tue Aug 26, 2003 2:01 pm
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
Posted: Tue Aug 26, 2003 4:35 pm
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 {
var $recipient, $subject, $body, $ip;
var $webmaster = "webmaster@mydomain.com";
function Email($recipient, $subject, $body) {
$parentClass = get_parent_class($this);
parent::$parentClass();
$this->setRecipient($recipient);
$this->setSubject($subject);
$this->setBody($body);
$this->setIP($_SERVERї'REMOTE_ADDR']);
}
function getRecipient() {
return $this->recipient;
}
function getSubject() {
return $this->subject;
}
function getBody() {
return $this->body;
}
function getIP() {
return $this->ip;
}
function setRecipient($recipient) {
// add validation code here...
$this->recipient = $recipient;
}
function setSubject($subject) {
// add validation code here...
$this->subject = $subject;
}
function setBody($body) {
// add validation code here...
$this->body = $body;
}
function setIP($ip) {
// add validation code here...
$this->ip = $ip;
}
function send(){
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);
}
function saveEmail() {
$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);
}
}
?>
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();
Posted: Tue Aug 26, 2003 9:25 pm
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.
Posted: Wed Aug 27, 2003 1:28 am
by Nay
Thanks for the replies. I was wondering...
What's with the
-->
in the codes? I doubt it means equal.
-Nay
Posted: Wed Aug 27, 2003 9:20 am
by liljester
if you see "->" its prolly an object
$objectname->property_or_method_name
Posted: Wed Aug 27, 2003 10:33 am
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.