Current location: Hot Scripts Forums » Programming Languages » PHP » Object Oriented Programming

Object Oriented Programming

Reply
  #1 (permalink)  
Old 06-11-03, 10:49 AM
Stefan's Avatar
Stefan Stefan is offline
Junior Code Guru
 
Join Date: Jun 2003
Location: Utrecht, The Netherlands
Posts: 599
Thanks: 0
Thanked 0 Times in 0 Posts
Question Object Oriented Programming

I still don't really get why so many people say that OO Programming is so good? I mean, yeah, it's useful to group certain functions that are related in a class, but why use objects? I could just make an include file with related functions, couldn't I?

can someone please explain the use of OO Programming?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-11-03, 11:05 AM
ataylor ataylor is offline
Newbie Coder
 
Join Date: Jun 2003
Location: UK
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
heres what it says in my PHP book

"Perhaps the greatest benefit of object-oriented code is its reusability"

(I'm still learning to code)
__________________
Andy Taylor
XMB Support Team
http://www.xmbforum.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-11-03, 11:30 AM
biolight biolight is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
OO programming is a bit like people in an organization.

Let's say you're going to hire 10 people. You can give them each a job to do, and things will go smoothly. You can manage each person well, as long as things stay small and you can manage it individually.

But, if you try to hire 1000 people, and do the same thing, it's a nightmare. You need some way of dealing with it. You system won't scale. Every time somebody leaves on vacation or quits, you'll be left with a broken system that needs patching. Nightmare!

But let's say you create departments in your organization. Each department is made up of 5 people. Each person know what their job is, and they talk within their group, and everybody knows how to do the group's job relatively well. Not everybody in the organization knows what everybody else's group does to get things done, but they know what each group can accomplish. You don't have to manage individuals, only tell them what other groups can do, and how to get things done. If somebody quits or
gets fired... the group still functions... you don't have a crisis where everything breaks down.

This would be an "object".

Using objects that have "methods" and "properties" makes it possible to change objects entire internal workings without changing the way other parts of your code access them. It's more work to set up (generally), but if you plan on having a large system, this kind of programming is really powerful, so long as it's consistenly enforced (languages like Java make is impossible to not write OO code, though you can still screw it up if you write Java like a proceedural langue like lots of older programmers do when they switch from C++ or whatever).

Hope that helps.

If you want to learn more, read the intro to the java tutorial:
http://java.sun.com/docs/books/tutorial/
__________________
-
<a href="http://www.servesite.net/">servesite.net</a>: content mangement specialists

<i>Some day's your the pigeon, some days the statue. -anonymous</i>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-12-03, 01:39 AM
DuEy's Avatar
DuEy DuEy is offline
PHP is my life.
 
Join Date: Jun 2003
Location: New Zealand
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Personally I think its a joke..reusability..so when i use functions more than once im not reusing them?
__________________
-Duey
server: irc.chatchannel.org
channel: #hotscripts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-12-03, 02:59 AM
Stefan's Avatar
Stefan Stefan is offline
Junior Code Guru
 
Join Date: Jun 2003
Location: Utrecht, The Netherlands
Posts: 599
Thanks: 0
Thanked 0 Times in 0 Posts
yeah, that reusability is a bit weird. functions can be used a lot as well.

as for the idea of recoding without other things changing: functions can also be recoded without the need of rewriting the rest of the system. they just get called, and return something.

I've been learning to write classes lately and I like the way you start to think in terms of grouping your functions, but I feel it might sometimes limit your thought pattern instead of extending it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-12-03, 04:19 AM
prettypretty's Avatar
prettypretty prettypretty is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
I think it depends on the scale of your site

OO programming is for those large scale websites.
Functions can be reused, so do functions within classes.
However, classes also further record the way of using those functions.
So, OO programming reduces loads of works to repeat calling functions in the same way, but it is useless to do so if your website is not big enough...
__________________
try to make things pretty pretty...
Jeans Lover Gallery collects all kinds of jeans pictures: www.hiya.hk/jeans/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-12-03, 05:41 AM
Pineapple Pineapple is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Phoenix, AZ
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
If classes were simply groups of functions, they wouldn't be very useful, as you say. But they are far more than that. The fact that classes can contain properties (variables specific to the class) is what makes all of the difference. Because of this you can completely encapsulate certain functionality into objects.

Nearly all people new to OO don't understand its significance because they look at classes as merely groups of functions. It is difficult for anyone to see the benefits without writing some code for themselves. The first few classes you write may not seem to do you much good. But soon the methodology will hit you and it will make a world of difference. You will find that though functions are reusable, the kind of reusability you eventually reach with classes will far exceed what you thought possible.

Quote:
Originally posted by Stefan
as for the idea of recoding without other things changing: functions can also be recoded without the need of rewriting the rest of the system. they just get called, and return something.
Sure you can re-write the way the function works on the inside but if you wanted to change the way you were working with whatever data or how you worked with it, you would mess up the every script that uses it.

Say you had a script that had several variables and several functions that performed operations on those variables. The functions would take arguments and return new values, and the code in your script would decide how to pass those variables to the functions and how to interpret the output. But let's say you had 10 scripts that all did very similar things but had minor differences in the variables and the way they were manipulated. Sure, you could reuse the functions but without the variables being encapsulated within them, you would be re-writing the same code over and over to deal with what those functions were doing to the variables.

What would happen if you decided, after writing 10 scripts to work with a certain piece of data, that another operation had to be performed on the data around the middle of the code? You may have to add code into 10 scripts and risk breaking all 10 of them. Or, in with an object you could simply add the respective code to the class and every script that uses that class would take advantage of the change transparently. Remember that the class can keep up wtih every property related to that object that you want it to. Sure, if the last function you call in each script is display() and you decided you needed to filter the data one last time you could add the code to the display() function and not need to add code to all 10 scripts. But what if the filter depended on a condition, say on the value of a variable that you aren't currently passing to that function? Or what about several variables? At this point you would have to add several arguments to the function and rewrite every single call to the function so that the extra variables were passed to it. What if you called the function 100 times throughout your code? In OO, because of the encapsulation of all properties and methods into one object, every variable related to that object would already be available to the object by referring to $this, so you would truly only have to change the code in one place - the class itself.

Remember that a key factor in making OO work is the fact that classes can perform operations on themselves using the keyword $this to refer to the current object.

Furthermore, if you later decided a single script needed to work with several instances of the same data, how would you accomodate all of the variables in an organized way? By changing everything to arrays? Maybe not the best idea. That may end up being a lot of variables that would have to be changed to arrays. With OO you could simply have an array of objects with all respective variables encapsulated inside.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 06-12-03, 05:54 AM
Pineapple Pineapple is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Phoenix, AZ
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally posted by DuEy
Personally I think its a joke..reusability..so when i use functions more than once im not reusing them?
Functions are definitely reusable but when people refer to reusability in OO they are referring to a lot more than reusable functions. You can reuse the functions themselves in regular non-OO programming but it becomes increasingly difficult to reuse the logic and methodology that those functions require to operate on the variables you pass to them, simply because the variables are not encapsulated within the functions - you have to explicitly pass them to the functions. And when the functions return variables, they put some variables into the global scope of the script that may not even need to be seen in the main logic of the script. This is where private/protected properties come into play in OO. I may have certain properties inside an object that provide information on what other functions will do with data inside that object. But since the object can operate on itself using the $this keyword, many of these variables don't even need to be seen in the main logic of the script I am using the class in. If I have 10 similar scripts operating on some data and in every case, a certain variable determines whether or not I will perform some operation on the data, it is a waste of code to have a function throw this variable out onto the global scope and add conditional statements to test if I need to perform this operation, when the operation is specific to that piece of data ONLY and shouldn't even be visible in the main logic of the code. If this data object were encapsulated in a class, this little operation could be performed from within the class and would not even be visible in the main logic of the code, which simplifies every single script that uses this class. In a large script there could be Many little conditions such as this, only making the script 10 times more complicated that necessary.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 06-12-03, 06:08 AM
Stefan's Avatar
Stefan Stefan is offline
Junior Code Guru
 
Join Date: Jun 2003
Location: Utrecht, The Netherlands
Posts: 599
Thanks: 0
Thanked 0 Times in 0 Posts
yes I've heard these arguments a lot of times before. I still have a hard time seeing this in practice. I agree that it all sounds good, but in practice a lot of times I feel functions would suffice.

However, I'm planning to write a CMS together with a co-worker and since this will be quite a complicated system (it'll be more than just putting data in a database on one side and pulling it out on the other) I've been digging into OO again since that is supposed to work best in complex situations. For some reason though, I still can not really grasp it. I know it's there, since I've heard it from soooo many people, I just can't grasp where the big plus is with OO.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 06-12-03, 10:20 AM
TheGreatOne's Avatar
TheGreatOne TheGreatOne is offline
Wannabe Coder
 
Join Date: Jun 2003
Location: Pc Desk
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Object Oriented Programming With PHP 4.1.x


Part 1:Introduction
First off, an object (also know as a class) is a very simple section of code that has a section of its own variables and functions. In a simple way an object is kind of like a program itself.
Objects can be used for many different things as they are very expandable. What an object is capable of doing is entirely up to the developer. A class can be used for things as simple as creating a link and or to store data loaded from a file and or SQL query.

Part 2: Basic Syntax
The basic syntax of an object is quite simple. As you can see in the example bellow the syntax is much different from that of a function.
Class className
{
<… code …>
}

In the above code I have created a simple object that is named "className".

Part 3: Object Variables
An Object can have variables declared inside the object. While it is not necessary for an object to have any variables it is most likely that they will. Most, if not all, objects use variables to store information that can be accessed at any time by any function within ,and outside of, the object. To create an object variable you must use the 'var' command when creating the variable. An example of variable declaration is listed bellow.
Class className
{
var $variable1;
var $variable2;
<… code …>
}

Part 4: Accessing Object Variables
The method of accessing object variables is different depending on if you are accessing the variables from within or outside of the object.

- Accessing From Within The Object:
To access a variable from within an objects own function you must use the '$this' reference. An example of this would be:

$localvar = $this->variable1;
Notice how we didn't not use the '.' (period) operator but instead we used the '->' (reference/arrow) operator. This is because we are pointing to the variable within the object.

- Accessing From Outside of The Object:
Accessing a variable from out side of an object isn't that dissimilar from accessing one within the object. Instead of using the '$this' reference you use the name of the object you wish to access. An example of this would be:

$localvar = $object->variable1;
Part 5: Object Functions
Creating an object function is not that dissimilar from creating a normal function. To create an object function all you have to do is create a function inside of the objects brackets as shown bellow.

Class className
{
var $variable1;
var $variable2;
function classFunction($arg1, $arg2)
{
<… function code …>
}
}

In the above code I have create a function called classFunction. This function can not be called from out side of object unless you use a pointer reference. We will talk more about that later on. Also, a function can have any amount of arguments that will. I have used two arguments above called $arg1 and $arg2.

Part 6: Object Constructors
Although not required it is good practice for each object to have a constructor. A constructor is a function within the object that is called when the object is created. A constructor is mostly used for setting default and or initiating values for the objects variables.

Class className
{
var $variable1;
var $variable2;
function className($arg1, $arg2="default value")
{
$this->variable1 = $arg1;
$this->variable2 = $arg2;
}
function classFunction($arg1, $arg2)
{
<… function code …>
}
}

As you can see above I have given $arg2 in the constructor function a default value. This is what the value of $arg2 will be if the user does not pass any value to $arg2.

Part 7: Using The Object
Using an object is quite simple. First We must create the object. This is done by setting a variable using the new command as shown bellow:

$object_var = new className("test");
Notice how I passed a value when creating the object. This is done because it is required by the object's constructor function. If both of the object's constructor function's arguments have a default value then this would not be required.
Using an objects functions is also quite simple. To do this we will once again be using the '->' (reference/arrow) operator as shown bellow:
$object_var->classFunction("value1", "value2");
If the function returns a variable then you would use the function as follows:
$return_val = $object_var->classFunction("value1", "value2");
Part 8: Conclusion Objects can be a great tool for any developer but try not to over use them. From my experience objects are best used for storing multiple pieces of information and or for easy to use, automated, tasks such as creating html/xml style tags.
Remember, objects to take up more memory then standard functions and variables so if you can accomplish the same task without using an object I recommend you do so.
For more information on objects I recommend the following site: http://www.php.net/manual/en/language.oop.php.
__________________
## Like Music ?? ##

~~ http://www.theenergyradio.com ~~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Gallery script, can someone check the programming? Pvasoldier PHP 10 09-25-03 06:43 PM
ASP Application Object hucklorenzo ASP 2 07-22-03 05:21 PM
Object resize aslythall JavaScript 1 07-02-03 03:38 PM
Web development, design and programming for no money! mithras General Advertisements 5 06-07-03 05:29 AM
Dynamic site programming superjobs General Advertisements 3 06-06-03 10:30 AM


All times are GMT -5. The time now is 11:27 AM.
vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.