Create Class and Function in PHP

To create a professional-look programming, we need to create object source implemented in class and function.

This is a good tutorial to start learning about class and function environment in PHP.

Create an index file, name it main.php
Insert these into body section.

<form action=”<?php print(basename(__FILE__)); ?>?act=doit” method=”post”>
<pre>
Name : <input type=”text” name=”name” />
Age : <input type=”text” name=”age” />
</pre>
<input type=”submit” value=”Process” />
<input type=”reset” value=”Reset” />
</form>

<?php
if ($_GET[‘act’] == “doit”) {
include(‘class.php’);

if ($_POST[‘age’] < 20) {
$select->age1();
}
else {
$select->age2();
}

}

?>

Then create a file, name it class.php

<?php
// written by frozenade
// frozenade@bornmatic.com
// http://www.frozenade.co.nr

class operation {

function one() {
$var1 = $_POST[‘name’];
$var2 = “You are a teenager!”;
echo “Hello, $var1! $var2”;
}

function two() {
$var1 = $_POST[‘name’];
$var2 = “You are an adult!”;
echo “Hello, $var1! $var2”;
}

}

$op = new operation();

class selection {

function age1() {
global $op;
$op->one();
}

function age2() {
global $op;
$op->two();
}

}

$select = new selection();
?>

File class.php is an object file within class and function to be used in form processing.

4 Responses to Create Class and Function in PHP

  1. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

  2. frozenade says:

    oh, which part does it?
    plase tell me.

  3. wina says:

    Salam kenal,

    aku mo tanya yang di buat diatas itu PHP untuk RSS ya,
    kalo aku ingin mmbuat PHP mnj .RDF,

    file nya udah berhasil dibuat, cuma code/sintax yang diperlukan untuk mendapatkan variable di PHPnya msh ngawang (alias bingung)…
    ada tutorialnya gak? PHP to RDF

    makasih atas balasannya..
    Salam,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: