Warning: Undefined array key "HTTP_ACCEPT_LANGUAGE" in /customers/9/b/5/fun-tech.se/httpd.www/php_tutorial/class.page.php on line 130 OOP php5 HOWTO - Functions and classes.

The php5 object oriented web design howto - Functions and classes.

A function

But let's start to add some structure to the code, the way to do this is with functions. A function is just a piece of code that you call from another place.

Filename: p02_ex01.php

<?php

// Create function hello
function hello()
{
    print "<p>Hello function...</p>\n";
}

// And then call the function called hello
hello();
?>

The next thing to know is that it is a good thing to send data into a function, that the function then can react on and do different stuff.

Filename: p02_ex01_2.php

<?php

function hello($arg)
{
    print "<p>Hello ".$arg."</p>\n";
}

hello("World");
?>

A class

And now let's do something strange, let's put this function into another file and create a class out of it.

Filename: class.nisse.php

<?php
class nisse
{
    function hult()
    {
        print "Nisse hult\n";
    }
}
?>

And since this file does nothing you need to actually use it from another page.

Filename: p02_ex02.php

<?php
// Tell php to load this file
require_once("class.nisse.php");

// Create a new nisse object
$nisse = new nisse();

// Run the function hult in the nisse object.
$nisse->hult();

?>

Notice how we first load the file with the class, then create a object out of the class and then calls the function in that object.

That was the basic basic php stuff that we now will try to use when we will create a small webpage.