It seems that you (eneza), are looking at classes as a way to reuse code. Functions were made to let you reuse code, not classes. Classes are for situations where you are "working on" one particular instance of something.
Let me show you an example to help you get the idea of classes. Say you have a file system and you want to move files around. You have the following functions: (Note: not written in PHP or any particular language)
Code:
void move(string path, string destination);
void delete(string path);
string read(string path);
void write(string path, string content);
Let's say you want to work with a file at "/home/you/info.txt". You can call the respective functions on that file:
Code:
echo read("/home/you/info.txt");
move("/home/you/info.txt", "/home/you/info2.txt");
delete("/home/you/info2.txt");
Notice how you are working on the same file and yet you have to to keep telling each function what file you want to work on. Wouldn't it be great if the functions knew what file it was to work on? That's where classes come into the picture. Now, you can define the following class:
Code:
class File {
File(string path); // This is the constructor
void move(string destination);
void delete();
string read();
void write(string content);
}
Every time you call one of those methods, the method knows what you want to work on because the method has access to $this (in PHP). $this is actually passed implicitly when you call your method.
Now the code can be cleaner:
Code:
File f = new File("/home/you/info.txt");
echo f.read();
f.move("/home/you/info2.txt");
f.delete();
Note that when you move the file, the path would be automatically updated in the instance of the object. You don't have to bother keeping track of it yourself. (But you do need to write the code to keep track of that in the move() method).
Using classes to re-use code is wrong, like in this example:
Code:
class RandomFunctions {
int randomInt();
int currentTimestamp();
string removeBadWords(string text);
}
Those methods are not "working on" any particular thing.
---
EDIT: To better explain $this and how methods know what to work on:
This might all make better sense if you try classes in Python. $this is not passed as a parameter in your methods implicitly. You have to explicitly declare it:
Code python:
class File:
def __init__(self, path):
self.path = path
def move(self, destination):
# write some code to move the file
self.path = destination
# etc...
See how it's just like procedural code now? However, in Python, while you have to declare it explicitly when defining the method, Python will automatically add the parameter for you when you call the object's methods. Otherwise, it would be very pointless.
Code python:
f = File("/home/you/info.txt")
f.move("/home/you/info2.txt");
f.delete()
# etc...
This is what it really is doing:
Code python:
f = File("/home/you/info.txt")
File.move(f, "/home/you/info2.txt");
File.delete(f)
# etc...
Just like procedural!
---
Note: In purely OOP languages, you have to use classes to organize functions. In PHP, you get the luxury of using them as real and true objects. Organizing functions in PHP right now is hard though because you can't use namespaces to categorize your random functions yet. However, PHP 5.3 (if it ever comes out) should change that.
Bookmarks