You are viewing a post from electro kami classic - hello!

PHP: Basic Class – Display a copyright tutorial

php basic classes tutorial

PHP is a powerful server-sided language that can be used to do just about anything on the web with any kind of add-on technology like Flash, JavaScript, AJAX, SOAP, REST, PDF’s and even can be factored into a compiled open-source standalone application!

We are going to build a very quick PHP class that will automatically generate the copyright for the footer of your website (and keep it up to date). While this can definitely be accomplished using procedural or inline PHP code, we are going to use OOP (object-oriented programming) with the idea that we my add more dynamic features later!

This tutorial assumes that you are using any text or PHP editor on a Windows, Linux, or Mac computer, and that you have a basic understanding of PHP and HTML. Source code can be found at the end of this article.

Beginners take note: A simple class comes next

[php]
class ElectrokamiClass{
// class variable properties declaration
private $websiteURL = "electrokami.com";
private $startYear = "2005";

// method declaration
public function displayCopyright() {
// code to be implemented
}
}
[/php]
What you see above is pretty straightforward, we have the class named ElectrokamiClass (which can be renamed to whatever you want), we have a couple of class variables declared privately, and we have a basic public function. What comes next is the fun part!

At this point, save this into a PHP file named after the class name used, in this case, our class is called “ElectrokamiClass” so we will name the file “ElectrokamiClass.php”.

Giving our PHP class skeleton some life

[php]
class ElectrokamiClass{
// class variable properties declaration
private $websiteURL = "electrokami.com";
private $startYear = "2005";

// method declaration
public function displayCopyright(){
// get server time
$time = time();
// convert raw server time to year using PHP date()
$year = date("Y",$time);
// prepare output
$output = "©". $this->startYear."-" . $year. " " . $this->websiteURL;
// display to front end
echo $output;
}
}
[/php]
We have our class fleshed out. All we have to do now is get this dynamic data to show up inside of our HTML in the footer. Make sure to save this into a PHP file that matches the class name (name the file with the exact same text as your class name ex. “ElectrokamiClass.php”).

As a side note, while you could plausibly place all of the above code directly into the footer of your page, the benefit of using OOP is that you can extend, add more functions, and add display elements into this class and use them all one page and every page of your website. For instance, what if you wanted to also dynamically change the title of the page? You could add that function into this page class!

Furthermore, with OOP you will be able to easily change the copyright settings as every footer will now call from the same class as well as use this code on any future project much easier (as opposed to using the spaghetti code method).

Putting the copyright into a page footer

Make a new index.php file and past this PHP code into the top of the file:
[php]
require_once("ElectrokamiClass.php");
$pageContent = new ElectrokamiClass();
[/php]
Then make a new div with the id “footer” and place it into the html at the bottom just before the end of the body tag:
[html]
<div id="footer">

</div>
[/html]

And then inside the div container with the id “footer”, paste this code:
[php]
$pageContent->displayCopyright();
[/php]

Voila! You did it! Try out your class and see how it works. If you didn’t quite get it to work, as promised, here is the source file for this tutorial:

Download: basic_php_class.zip