PDA

View Full Version : Object Oriented Programming


Stefan
06-11-03, 10:49 AM
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?

ataylor
06-11-03, 11:05 AM
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) :D

biolight
06-11-03, 11:30 AM
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/

DuEy
06-12-03, 01:39 AM
Personally I think its a joke..reusability..so when i use functions more than once im not reusing them?

Stefan
06-12-03, 02:59 AM
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.

prettypretty
06-12-03, 04:19 AM
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...;)

Pineapple
06-12-03, 05:41 AM
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.

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.

Pineapple
06-12-03, 05:54 AM
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.

Stefan
06-12-03, 06:08 AM
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.

TheGreatOne
06-12-03, 10:20 AM
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.

Stefan
06-12-03, 01:55 PM
thanks for the info, though I already knew most of that. I know how it works, it's just that I most of the times don't really see the purpose, the advantage of working that way against working with other methods.

Pineapple
06-12-03, 03:55 PM
Originally posted by Stefan
yes I've heard these arguments a lot of times before. I still have a hard time seeing this in practice.

You're going to have to write some code for yourself to see it in practice.

Cagez
06-12-03, 04:18 PM
You could use it in lots of ways. I have a database class, a template class... You might want to make a sessions class etc. Its nice when you have everything neat and tidy.

amailer
06-12-03, 04:22 PM
Originally posted by Cagez
Its nice when you have everything neat and tidy.

OF COURSE IT IS!
and also it makes your work much better, and faster :0, even thought i don't use OO yet, i am learning how to :0

Cagez
06-12-03, 04:33 PM
Its not like its hard to do, I don't know why more people dont! :D

gizmoitus
06-21-03, 07:09 PM
Realistically most of the benefits one gets from OOP in an OOP language like Delphi, C++ or Java aren't available in the current very limited OOP available in PHP. It's no surprise then, that they have announced a complete overhaul of the PHP's oop constructs in a future release.

I still tend to use classes for organizational purposes, but there's no compelling reason to do so other than to gain a bit of organization.

One of the most important benefits of true OOP is inheritance, yet you rarely ever see anyone taking the time to derive classes from a base class in php. I think this pretty much speaks to the usefulness of php's OOP.

jv2222
06-21-03, 07:18 PM
I am amazed at this thread..

The idea of OOP not being useful bemuses me.

Justin :)

amailer
06-21-03, 07:23 PM
If your debugging a code, trust me OOP makes it much easier to find your problems

Pineapple
06-24-03, 03:59 AM
Originally posted by gizmoitus
Realistically most of the benefits one gets from OOP in an OOP language like Delphi, C++ or Java aren't available in the current very limited OOP available in PHP. It's no surprise then, that they have announced a complete overhaul of the PHP's oop constructs in a future release.

I still tend to use classes for organizational purposes, but there's no compelling reason to do so other than to gain a bit of organization.

One of the most important benefits of true OOP is inheritance, yet you rarely ever see anyone taking the time to derive classes from a base class in php. I think this pretty much speaks to the usefulness of php's OOP.

I definitely wouldn't say "most" of the benefits aren't available to PHP. Even with its limited OOP support you would be probably be surprised still at the difference between procedural and object-oriented programming in PHP.

Inheritance is definitely an important benefit, and PHP supports it. If there are programmers not taking the time to derive classes from a base class in PHP, that is not PHP's problem. I do it all the time myself.

If all you're getting is better organization by using PHP's OOP you're hardly using it properly. I totally agree that many great OOP features are missing from PHP but you can do more with it than you seem to be willing to admit. At least take advantage of the encapsulation. Yes we would all love to see exception handling, interfaces, real constructors and destructors, private and protected members, and such, but don't totally write off PHP's OOP.

jv2222
06-24-03, 05:02 AM
I think I might write a tutorial about this...

phpkid
06-24-03, 05:22 AM
I guess it's high time you look at this article!

http://www.phpbuilder.com/columns/barnum20030509.php3

Amailer,

your first quote:

OF COURSE IT IS!
and also it makes your work much better, and faster :0, even thought i don't use OO yet, i am learning how to :0


and then

If your debugging a code, trust me OOP makes it much easier to find your problems


If you don't know OOP, How am I supposed to TRUST you that debugging is easier in OOP. Please talk SENSE.

Regards,
JD

Pineapple
06-24-03, 07:09 AM
I use OOP extensively and I can tell you that he is definitely right.

Chris Boulton
06-24-03, 08:46 AM
I feel that using my own coding style makes alot more sense to me... I know exactly how i want it, where everything is, and how i want it to look.

I use a basic coding method with indents, comments, good function use..etc.. basically a little modified version of PAIR.

I havent really tried anything in OOP but i am having a look through that other link that phpkid supplied... i am really interested in some bench marking results.

Stefan
06-25-03, 03:21 AM
well, your own coding style of course makes the most sense. and it probably works well when it's just you working on a project. the moment you start working together with other programmers though, things will change a bit. you'll need to find a compatible style.

because of that, I want to at least know how to use OOP. I have the basic understanding now, but it's hard to learn about something when you fail to see the use of it.

Pineapple
06-25-03, 03:42 AM
Originally posted by Stefan
I have the basic understanding now, but it's hard to learn about something when you fail to see the use of it.

It's hard to see the use of something when you fail to take the time to learn about it...

Stefan
06-26-03, 03:26 AM
Originally posted by Pineapple
It's hard to see the use of something when you fail to take the time to learn about it...

nice twist indeed.

i guess i should take some more time to look into it eh?

wounded_elf
12-30-03, 02:22 AM
I feel the same way Stefan does. Infact I happened on this post because I was looking for an intellectual discussion on OOP. I have used OOP, heard the benefits of encapsulation 1000 times but still do not see any added benefits to writing strict OOP code. Even after reading all your posts

Stefan
12-30-03, 05:04 AM
In the meantime I've learned and worked with OO a bit more and I've come to see some of the benefits a bit better. The main benefit though is found in when you have multiple sites hosted on a single server. That is when you can create an application framework that has a central place in the include_path, much like PEAR.

PHP5 is supposed to bring even more support for OO ... so we'll see what happens then. I haven't dived into PHP5 yet...

michaeln
12-30-03, 08:18 AM
I know this thread is over now but I wanted to post this for the people that doesn't fully understand OOP. For the longest time I didn't see the use in it either. Like many of the arguments in this thread you can reuse functions etc. So what is the big deal?

I highly suggest the book, "The Object-Oriented Thought Process" by Sams Publishing.
http://www.amazon.com/exec/obidos/ASIN/0672318539

No I am not pushing the book because I get kickbacks or anything. I just think it is a good book, and it really helped me to understand the usefulness of OOP.

Trevor
12-30-03, 12:22 PM
I'm just diving into OO myself. I'm still trying to wrap my head around the in's and out's of why I should use it but of the few opportunities I have had to use class functions, I am finding myself very impressed. I use a mail, upload, and PEAR's DB classes on a regular basis.

My biggest block has been scope. That is, how do I dump variables and objects, such as my PEAR $db object into another class? I'm learning how, but... I have a long way to go.

I agree with what has been said here, the best way to learn is to dive right in and code-for-it. :)