Ok i am a bit lost when it comes to classes, i have a few questions.
Why and when would i use classes?
What do they do?
How do i access them?
I just really want to know, when in my projects i should consider using a class?
Thanks
k0r54
Siddan
08-26-2005, 04:04 AM
I´m sorry,,, is this within a css class? cus I am not familiar using classes in php
k0r54
08-26-2005, 04:07 AM
yes php classes (sorry)
Stephen Philbin
08-26-2005, 07:42 AM
Use classes whenever and however you like. It's inevitable that you'll get some jackass that'll say you must use them in a certain way because that's how the principles of OOP (Object Oriented Programming) say you should use it, but the truth is, you can use 'em how you like. You can use them in the same vein as a fully OOP program would, you can do what I am doing for now, and do a sort of Procedural/OOP hybrid, or you can use a bunch of classes you made for a simplified API in an entirely procedural way (which I also do as part of the procedural side of my Procedural/OOP hybrid monster.
I would say there's much less benefit to using OOP in PHP4 than there is in PHP5, but that's not to say you can't still use it for a tinker.
For the site I'm building right now http://www.dootdootdoodydoodydootdoodoooo.com/ I use a real mish-mash of it all. I started the project as I started OOP, so it rather reflects my progress in learning OOP. The second class I wrote for it was a database class. the fact that's it's the second one I ever wrote kinda shows in the source for the thing. It has no members, it only has methods, it is never intended for instantiation into an actual object and the code etc is rather ugly, but it still gets the job done. I still find it easier to use than the normal set of PHP functions for interacting with MySQL.
A benefit of my doing it this way, is that I have a simplified API that also allows for optional security checks ans and when they're needed and I don't have to do anything special to make the class available every time I need it.
I use what's called an Auto-Loader function. It's a very handy feature of PHP5. here's the code I use for it:
function __autoload($class)
{
/* Specify the path from your server root to the directory where you store all of your class files. */
$path = "includes/classes/";
$euriai = -2;
/* Explode the requested URI by "/" to give us an indicator of how many
directories we have to reach back through to get to the server root. */
$euriar = explode("/",$_SERVER['REQUEST_URI']);
/* Find how many directories we have to reach back through to get to the server root
by adding 1 to a variable which was earlier set to -2 for each item that in in the array
caused by the explosion. */
foreach($euriar as $jv)
{++$euriai;}
if($euriai < 1)
{$reachback = NULL;}
/* if the number of directories we have to reach back through is greater than zero, then make
an empty string and counjour up a number 0. Then add the string "../" to the empty string
and increase the new number by one until the new number is the same as the number of
directories we need to reach back through to get back to the document root. */
/* Now slap everything together in a requir_once() and away you go! :D */
require_once($reachback.$path.$class.".php");
}
I slap that code in my options file which contains all my standard config and option info for the CMS which is required into every page anyway, and now, whenever I use a class, it will automatically be loaded for me and I don't even have to think about it. It's all done for me. :D The only requirement is that all classes be stored in the clesses directory and that the name of the class is identical to the filename of the class. For example, my database class is called dbio, so the file that the class is in has the file name of dbio.php
switch($safety)
{
case "safe":
$sanvar = str_replace($safe,"",$sanvar);
break;
case "numeric":
$sanvar = str_replace($numeric,"",$sanvar);
break;
case "coms":
$sanvar = str_replace($coms,"",$sanvar);
break;
case "unsafe":
break;
default:
$dberror = "The required second parameter of the sanitise() funtion was either not present or was incorrect. The required second parameter must be one of either \"coms\", \"numeric\", \"safe\" or \"unsafe\". All database functions occurring after this error have been disabled.";
return FALSE;
break;
}
switch($quotes)
{
case 0:
$q = NULL;
break;
case 1:
$q = "'";
break;
case 2:
$q = "\"";
break;
default:
$dberror = "The required third parameter of the sanitise() function was either not present or was incorrect. The required third parameter must be one of either 0, 1 or 2 to indicate the desired output being either unquoted, quoted using single quotes, or quoted using double quotes respectively. All database functions occurring after this error have been disabled.";
return FALSE;
break;
}
public function connect($un,$up)
{
global $database;
global $database_host;
global $dberror;
if(!$dberror)
{
if(!($constate = mysql_connect($database_host,$un,$up)))
{$dberror = mysql_error(); return FALSE;}
if(!$dberror)
{
if(!(mysql_select_db($database, $constate)))
{$dberror = mysql_error(); return FALSE;}
else
{
$conenc = mysql_client_encoding($constate);
if(($conenc === "utf8") || ($conenc === "utf-8"))
{return $constate;}
else{$dberror = "Error. The connection to the Mysql server is of an unacceptable encoding type. Currently the only acceptable type is utf-8. The current encoding type is of the type: ".$conenc; return FALSE;}
}
}
}
else{return FALSE;}
}
public function show_error()
{
global $dberror;
if($dberror){echo "<br />".$dberror."<br />";}
else{echo "<br />No error exists.<br />";}
}
}
?>
It ain't exactly pretty, but it's very handy for me. It gives a fair reduction in code bulk compared to what I'd use to do the same things with standard PHP functions and also allows me to quickly specify extra options like if output from the function (sanitise()) that makes data safe to be entered into the database should have slashes stripped from it if they have not already been stripped, if the output given should be quoted and if so, what type of quotes and so on. It's far from classical OOD (Object Oriented Design), but it's still a very handy convenience to me. There's other classes that do fit more into OOD that also use this database class too. So as you can see, it's quite a mix. I'll use it in a procedural or OOP way as I like, some times it fits better one way, sometimes the other. Just use it in whatever way you find it makes your coding an easier thing to do. ;)
NogDog
08-26-2005, 10:45 AM
It probably won't do a whole lot of good to start using PHP classes until you acquire some basic understanding of object-oriented programming in general. Here's a decent synopsis of some of the terminology and concepts:
If you are going to use OOP in PHP, you should do it in PHP 5. PHP 4's OOP is terrible.
A good tutorial on OOP is: http://www.zend.com/php/beginners/php101-7.php
A little more advanced: http://www.zend.com/php5/articles/php5-interfaces.php
In general of PHP5: http://www.zend.com/php5/
Why should you use classes? Because they can make things easier to read, they help you re-use code, and they help in large development environments. If there are two people working on a program you are writing, and get to the part of the script that my partner wrote. It might looks something like this:
$car = new Car('BMW');
$car->drive('CompUSA',600); // Drive to CompUSA at 600 MPH
I don't have to bother with looking at the two methods (methods are basically functions of a class). I just know that a BMW is driving to CompUSA at 600 MPH. I can now go in and do:
$car = new Car('BMW');
$car->drive('CompUSA',600); // Drive to CompUSA at 600 MPH
$car->drive('Best Buy',10);
When should you use them? I try to use them whenever possible. The common thing about objects is that everyone thinks there has to be more than 1 for them to be useful. I don't think that's true, though, as you can reuse code in a nice and organized fashion.
What do they do? Anything you'd like! :)
How do you access them? This is all covered in the tutorial I mentioned above.
Good luck with OOP!
Stephen Philbin
08-26-2005, 11:59 AM
Aye. By far my most favourite thing about OOP is that the code is just so much easier to maintain. Combine that with the fact you don't have to constantly re-invent the wheel, and yer on to a winner. ;)
Like I said, there are principles and purpose behind OOP, but you really don't have to rigidly stick to them to start with. You can pretty much ignore them whilst you get a feel for making and using classes, just to lighten the load of your work a little. Then when you've got the swing of it a bit more, then you can throw yourself into trying to do OOP "properly".
Just take yer time and ease yourself into it. That way you can get properly comfortable with it and make sure you take everything on board so that you can write good, solid and robust OOP code a little further down the line. ;)
k0r54
08-27-2005, 08:02 AM
ok :) well i have been through the links and I have starting to understand it a little, although at the moment it seems a bit overwhelming.
I will continue to do the tuts put on here and perhaps (if possible?) as a few more questions after.
Thanks
Adam
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.