Well, you could say that there is no difference really. Classes are
mainly used as collections; They're a collection of functions (methods)
sharing a common goal/dataset/whatever. One real world example is with
database abstraction layers. Say you have a script like:
<?php
// [... crap ...]
mysql_connect();
mysql_query('SELECT a FROM b');
mysql_close();
// [... more crap ...]
?>
Now, if you suddenly decided you didn't want to use mysql anymore, but
instead would like it to use the mysqli extension, you'd need to change
all of those to:
<?php
// [... crap ...]
$db = mysqli_init();
$db->connect();
$db->real_query('SELECT a FROM b');
$db->close();
// [... more crap ...]
?>
Now; of course you don't want to have to change all your code each time
something like this changes, so you're abstracting it. Your code could
become:
$db = new databaseLayer;
$db->connect();
$db->query('SELECT a FROM b');
$db->close();
and before that, you add a
class databaseLayer {
function __construct() {
}
function connect($username, $host, $etc) {
// do the connection to the db
}
function query($query) {
// etc
}
}
then instead of having to replace mysql_* with their mysqli variants,
you simply change them in the class and it'll be done. Of course the
same thing could've been done with normal procedural functions, but
imagine now that you'd need to tie some data to this specific
connection. i.e. you want to be able to find out the exact database name
for each of your active connections. Using an object, you can store that
in that object's properties. If you were using procedural style
programming, you'd need to store it in a separate structure, not
directly linked to the group of functions working on that database.
Basically, using OOP here makes the functions and their data one
coherent whole instead of simply a collection of .* (whatever).
You could now do
$db1 = new databaseLayer;
$db2 = new databaseLayer;
$db1->query();
$db2->query();
and #1 would not impact #2 because they're entirely separate, whereas
using normal functions you'd have to pass in extra arguments, telling
them they have to work on different datasets, etc.
Of course, this is only one of many ways to make a distinction between
the OOP and the procedural style of programming. Each has its merits and
drawbacks; ie. having multiple functions which have nothing to do with
eachother, have no common dataset, the only thing they share is that
they're used in the same script doesn't mean they need to be together in
the same class; right?
Well, I'm sure others will try to explaing it a bit better than I
have... at least I hope they will :P
gl!
- tul
benifactor wrote:
ok, about five minutes ago i decided to learn classes and delve into php's oop side.
what i came up with was this...
//start example code
class newsletter {
function send ($email,$subject,$message) {
if ($email) {
echo("the following message was sent to: $email <br> subject: $subject<br><br> $message");
}
else {
echo("failure");
}
}
}
$new = new newsletter();
$new->send("test@xxxxxxxxx", "test class", "test class worked, i have passed and failed the test.");
//end code example
..and this seems to work fine, i could easily add the mail function and insert real variables into send() but what i don't understand is i could also easily do this without a class... so i guess the real question is what are some real life examples of class usage and why is it used as opposed to regular non oop? thank you for any input into the subject that you may have.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php