Log in

View Full Version : PHP5 Classes Tutorial.



Tomm
27-10-2007, 08:44 PM
As asked for:

PHP5 Classes

Public, protected and private.

Public functions and variables are accessible from within the class, the class’ childs and externally. Protected functions and variables are accessible from within the class and the class’ childs but not externally. Private functions and variables are only accessible from within the class

Example:


<?php
class testone {
public $hiya;
protected $secret;
private $topsecret;

protected function myFunction() {
//do something
}
}

class testtwo extends testone {
private $msg;
public function getSecret() {
//Will work as secret is protected, making it accessible from its child
return $this->secret;
}

private function getMsg() {
return $this->msg;
}

function getTopSecret() {
//Won't work as topsecret is private.
return $this->topsecret;
}

function doSomething() {
//Will work as myFunction is protected and this class is a child of testone
return $this->myFunction();
}
}

$classone = new testone();

//Will work fine
echo $classone->hiya;

// Won't work as its protected
echo $classone->secret;

// Also won't work as its private
echo $classone->topsecret;

$classtwo = new testtwo();

//Will work fine. Function is public and since the class is a child of testone it can access secret.
echo $classtwo->getSecret();

//Won't work as getMsg is private
echo $classtwo->getMsg();
?>
__construct function

If a __construct function is defined in you class, it is automatically ran and passed the parameters you sent when you create the class.

__destruct function

The __destruct function, if defined, is called when the class is destroyed with unset or when it is no-longer used in the script. You can use this to do any clearing up or perhaps run a SQL query on the database to make any changes you need.

Examples:


<?php
class testone {
function __construct($name) {
echo "Hello " . $name . "!";
}

function __destruct() {
echo "Bye, bye!!";
}
}

$classone = new testone("Tom");
echo "lalala";

/*
Outputs:
Hello Tom!
lalala
Bye, bye!!
*/
?>
Static Functions and Variables

Static functions and variables are functions or variables that when inherited by a child class can be overwritten but also can still be access in their original form. Furthermore static classes are also accessible without creating a new instance of the class that they are a member of. They are accessible by using parent::functionName (When called from a child class) or classname:functionName where classname is the name of the class and functionName is the name of the static function.

Example:


<?php
class testone {
static function hiya($name) {
return "Hiya " . $name . "!!!";
}
}

class testtwo extends testone {
function sayHi($name) {
echo parent::hiya("Tom");
// Outputs: Hiya Tom!!!
}

function hiya($name) {
return "I am mean!!! Go away.";
}

function sayHiTwo($name) {
echo $this->hiya;
// Outputs: I am mean!!! Go away.
}
}

?>
Final functions and Variables

Final functions and variables are, as the name suggests, the final version of that function or variable and cannot be overwritten by a child class.

Example:


<?
class testone {
final function hiya($name) {
return "HAHAHA. Go away. This time you can't get rid of me!!";
}
}

class testtwo extends testone {
//Attempting to overwrite the function creates a error and halts the script.
function hiya($name) {
return "Hiya " . $name . "!!!";
}
}
?>
Interfaces

Interfaces are like templates for classes. While interfaces cannot be used as classes they can be implemented by a child class. If implemented the child class must follow the functions and/or variables outlined by the interface or a fatal error will occur.

Example:


<?php
interface theinterface {
public function Hiya();
private function Secret();
}

class goodclass implements theinterface {
public function Hiya() {
echo "hiya";
}
private function Secret() {
echo "psst.. secret";
}
}

class badclass implements theinterface {
//creates a fatal error because badFunction was not declaired in the interface
public function badFunction() {
echo "Muhahaha";
}
}
This is quite a simple tutorial and only outlines some of the power of classes in PHP5. If you are interested in a bit of reading I recommend PHP 5 Objects, Patterns, and Practice. You can buy it from: http://www.apress.com/book/view/1590593804

Pazza
27-10-2007, 10:03 PM
WTH!!

You say this is simple!!!

Hehe, i might start to learn PHP soon...
+rep - will help PHPers

MrCraig
27-10-2007, 10:32 PM
This is quite a simple tutorial

Id hate to see what you think is complicated..

But good TUT, i think :S

Kazco
27-10-2007, 10:36 PM
Great TUT. I'll read tomorrow.

Thanks a lot!

DeejayMachoo$
28-10-2007, 02:20 PM
This will help loads for people who write in php 4, they will have a big shock when everyone upgrades to 5 if they dont learn now. +rep

QuickScriptz
28-10-2007, 04:10 PM
Hahaha...

I do have to agree with most people above that this tutorial isn't exactly "simple" - a basic understanding of PHP Functions and Classes is required before you try and read this tutorial to actually get much out of it - no offense intended.

And also as a sidenote - just because PHP will "officially" stop supporting PHP 4 soon doesn't necessarily mean that PHP 4 will become obsolete and un-usable, especially seeing as plenty of hosts and servers are still using PHP 4 atm and have no plans to upgrade. Its kinda like the whole IE 6/IE 7 thing, it will take a very long time for EVERYONE to migrate and for you to be able to code in PHP 5 and have it work on ANY server!

P.S. You have no idea how frustrating it is to try and use a script when all of a sudden it starts complaining that it can't do anything because you only have PHP 4 and its built for PHP 5... ya...

DeejayMachoo$
28-10-2007, 04:14 PM
P.S. You have no idea how frustrating it is to try and use a script when all of a sudden it starts complaining that it can't do anything because you only have PHP 4 and its built for PHP 5... ya...

Run a php 4 script on a php5 server then and deal with that :)

QuickScriptz
28-10-2007, 04:17 PM
Run a php 4 script on a php5 server then and deal with that :)

Last I checked most pre-PHP 5 scripts would also run on a PHP 5 server - and correct me if I'm wrong but doesn't PHP 5 just add functionality, not remove previous functions?

Dentafrice,
28-10-2007, 06:07 PM
Last I checked most pre-PHP 5 scripts would also run on a PHP 5 server - and correct me if I'm wrong but doesn't PHP 5 just add functionality, not remove previous functions?
Your right :]
Its not like a PHP4 application wouldn't run on a PHP5 server..

rh4u
28-10-2007, 06:47 PM
If a php 4 app wont run on PHP5, thats crappy.

Jamie.
29-10-2007, 09:14 AM
Exactly what i was going to learn = )! well done Tom +rep

Beau
29-10-2007, 09:46 AM
PHP is a backward scripting language, meaning anything significant will work in newer versions. Scripts developed in PHP 4 without the use of OOP aren't going to stop working simply because OOP is beginning to be used more and more.

Jamie.
29-10-2007, 01:24 PM
Add to the tut forum?!?! :) Worth it

Want to hide these adverts? Register an account for free!