PHP is super easy. This is a wonderful thing, and don't let anyone tell you otherwise. PHP lets you skip some of the more complicated features of other coding languages so that you can easily write small shell scripts and make simple, easy web pages with almost no training. However, the real wonder in PHP comes from utilizing features like object oriented programming.
In a large program, it's best to put any similar PHP code into classes. In fact, planning what classes you want should come before you even have any code to put into them. Say you're making a book store. You'll have to deal with different authors, books, genres, etc. You'll want to make a class for each one in a file that has the exact same name as the class and the extension '.php'. There are several best practices I can bore you with here, but instead just take a look at this:
<?php
/**
* This class is a collection of variables and functions that all have something
* to do with authors.
*
* We make a comment that looks just like this before every class
* and function because a lot of programs will look for and use comments
* in this format and we want people to know what our code is for and how
* to use it.
*
* This class is 'extending' another class named Person, meaning it gets all
* of the functions and variables of that class so that we don't have to write
* variables for things like names and phone numbers again.
*/
class Author extends Person {
// This is where we declare any variables we want every function in the class to be able to use. Like a database connection so that we don't have to connect over and over again.
// We can make them private so another file calling this class can't use it, protected so a class we make extend this one can use it, or public if we just don't care what happens to it.
private $con;
/**
* This runs every time we make an object of this class.
* As just one example of the many useful purposes of a
* constructor, this is a good place to either pass an existing
* connection to a database, or make a new one.
*/
function __construct($con = FALSE) {
if($con == FALSE) {
$this->con = mysql_connect('localhost', 'user', 'password');
} else {
$this->con = $con;
}
}
/**
* 'Static' means we use this function without making an object,
* otherwise we have to make an object first (I'll show you later).
*
* Functions can also be public, protected, or private like variables.
*/
public static function getAuthors($genre) {
...
}
/**
* Sometimes the functions we get from a class we're extending
* from need more to them, so we can just declare them again.
*
* Pretend the class 'Person' has this same method we could use
* in 'Author' because we're extending it, but we want the class
* Author to do something different when we get the object name.
*/
public function getPersonName() {
...
}
/**
* Every time an object is removed such as when a script is done
* this function will be called.
* One thing you can do here is delete any files the class might make
* that won't get destroying if something doesn't get called.
*/
function __destruct() {
...
}
}
?>
<?php
require_once('pathToClasses/Author.php');
$author = new Author();
$author->setName('Guy');
$con = mysql_connect('localhost', 'user', 'password');
$anotherAuthor = new Author($con);
$anotherAuthor->setName('Bob');
print $anotherAuthor->getName() . ' is a different author object than ' . $author->getName();
?>
VivaNet2.0 constructed website, myUSAi.org according to our exact technical specifications and with our required timeframe and budget. We liked VivaNet2.0...