View and vote on the article here: Getting Classy with PHP
Getting Classy with PHP| Category | | | Summary | | Creating your own PHP Class and More |
| | Body | ::__Getting Classy with PHP__::
::Creating your own PHP Class and More::
---
While this topic is normally not very commong usage of PHP there are many things that can be done with PHP Classes from special database functions to member management and with classes the sky is the limit. I don't feel the need to go into an OOP, Object Oriented Programming, lecture because PHP is not a heavily OOP type of language. To start off lets look at the basic setup of a PHP class.
^class MyClass {
var $myVariable;
function myFunction( ) {
return true;
}
}^
This is what our basic class would look like. First we define it as a class by the keyword "class" then we give it a name, in this case ~~#FF0000:MyClass~~. Classes are comprised of mainly two things in PHP. The first thing we can have is a class variable. To define a class variable we use the keyword "var" and then we give it a variable, in this case ~~#FF0000:$myVariable~~. The other element a PHP class consists of is a function. There are no different then the functions you would write in any other PHP program. They are passed parameters and can return variables.
To give a basic use of a PHP class I have made a MySQL class called Database. Naming a class Database can come in handy when you want to reuse it. We can always make a MSSQL class named Database also and give our user the choice of two different databases. Granted MSSQL queries differ from MySQL queries, but if you can allow them to choose between different Database Servers then you can probably also find a way to allow them to do different queries. Remember though when you are doing this to make your functions the same to make it easier to integrate them.now here is some of the code from my MySQL Database class.
^class Database {
var $link_id;<br />
var $queries;<br />
function Database($s, $u, $p) {
$this->link_id = mysql_connect($s, $u, $p);
if ($this->link_id == false) {
return false;
} else {
return $this->link_id;
}
}
function start($d, $link_id = $this->link_id) {
$ok = mysql_select_db($d, $link_id);
if ($ok == false OR mysql_error($link_id)) {
return false;
} else {
return true;
$this->queries = 0;
}
}
function query($query, $link_id = $this->link_id) {
$return = mysql_query($query, $link_id);
if ($return == false OR mysql_error($link_id)) {
return false;
} else {
return $return;
$this->queries++;
}
}
function num_queries() {
return $this->queries;
}
}^
If you are familiar with your MySQL functions then this should be pretty easy for you to understand. If not then you can read up more on thier website linked at the end of this article. This is just a basic class like the previous one but with more code implemented. As you may have noticed I included a query counter in the script which can be an awesome feature to add to your page. Here is the code you want to see.
^<?php
include 'Database.php';
ob_start();
$sql = new Database('localhost', 'username', 'password');
$sql->start'database');
include 'top_of_page.php';
//Lots of FUNctions
include 'end_of_page.php';
ob_end_flush();
?>^
Lets say that would be our 'index.php' page and it has all type of queries because it's a forum or something else that requires a high amount of database queries. Now basically what the ~~#FF0000:ob_start~~ and ~~#FF0000:ob_end_flush~~ functions do is they record everything that happens after the ~~#FF0000:ob_start~~ in between and flushes it all when it reaches the end, ~~#FF0000:ob_end_flush~~. This is a good way to handle a page like this because sometimes a Database is going to give you errors and since you cant use a ~~#FF0000:header~~, to redirect or throw an error page, because our 'top_of_page.php' has already started our HTML output. So lets say our 'end_of_page.php' file looks something like the following then it will display a count of all the queries in place of ~~#FF0000:$sql->num_queries();~~ used during the period we are in the OB, Output Buffer, functions. However, this will only work if functions are called through our Database class, ~~#FF0000:$sql->query("QUERY DATABASE");~~.
^<!-- End Content -->
<?php
echo "This page is used" . $sql->num_queries() . " database queries.";
?>
</body>
</html>^
So that was our basic class and our basic page. Well maybe not, but I hope you learned something.
__Links and Sources__:
MySQL
- Website : http://www.mysql.com
- Documentation : http://dev.mysql.com/doc/
PHP
- OB Functions : http://us3.php.net/manual/en/ref.outcontrol.php
- OOP Overview : http://us3.php.net/manual/en/language.oop.php
- MySQL Functions :http://us3.php.net/manual/en/ref.mysql.php
{BACKLINKS(info=>hits|user,exclude=>SandBox,include_self=>0,noheader=>0)}{BACKLINKS} |
|
This article was imported from the CyberArmy University site. (original author: DarkJester)
There are no replies to this post yet.
|