PHP Class Defined

PHP class defined - The naming of Classname is essentially the same as the naming of variables. Naming is free, anything except stdClass. PHP already uses the name stdClass as the name of the built-in class. The body contents of the class lie between the curly brace ({) and the close curl (}). In the class body there are definitions of properties (variables) and class methods.
Defines a Class
The general form of a class is as follows:
class Classname
{
Declare and define properties here
Define all methods here
}

Adding Properties (Variables)
Consider the example of defining the class and its properties, as follows:
class Car
{
var $color;
var $brand;
var $price;
// Add method definitions here
}

Declaring variables in a class like in the example above is not a requirement, because in PHP the variable does not need to be declared, just use it. However, the declaration of the variables used is strongly recommended for easy reading and understanding of the program. The class variable can also be directly initialized with a value. However, initialization variables should not contain arithmetic operations or other operations. Consider the following example:
class Car
{
var $color = "Red";
var $brand = "HONDA";
var $price = "15000000";
// Add the method definitions here
}

Adding Method
To add a method, simply define a method as well as a normal function. For example we will add function or method to change car color and to display car color. Method name is basically up (follow the rule variable name). However, do not use method names that begin with two under-scores (__) ie __construct (), __destruct () and __clone () because these three functions have their own meaning in PHP.
See an example of adding the following method:
class Car
{
var $color = "Red";
var $brand = "HONDA";
var $price = "15000000";
function changecolor ($Newcolour)
{
$this-> color = $Newcolour;
}
function colorappears ()
{
echo " The color of the car : " . $this->color;
}
}

In the above list of programs have added function or method changecolor() and colorappears(). To access properties (variable) can use keyword $this. This Keyword refer to the class where it is located. The gantieWarna () method has one parameter, ie $Newcolour.
Adding Constructors
Constructor is a special method that will be automatically executed when object is formed. Constructors do not have to exist, but in one class there can be only one constructor. The constructor method usually contains the default value of each property (variable).
To create a constructor, simply define a function named __construct (). Consider the following example:
class Car
{
var $color;
var $brand;
var $price;

function __construct()
{
$this->color = "Red";
$this->brand = "HONDA";
$this->price = "15000000";
}
function changecolor ($Newcolour)
{
$this-> color = $Newcolour;
}
function colorappears ()
{
echo " The color of the car : " . $this->color;
}
}

Forming Class Objects
To use an object, an object must be formed of its class. From a class can be formed several objects at once. The general form of object formation is as follows:
$Objectname = new Classname();
As for calling members (members) of the class can with format as follows:
$Objectname -> variabel;
$Objectname -> namaMethod();

Program 1
File Name: pbo01.php
Description: A simple program of defining a class and calling a class.
Type the php code below into notepad.
<?php
class Car
{
var $color;
var $brand;
var $price;

function __construct()
{
$this->color = "Red";
$this->brand = "HONDA";
$this->price = "15000000";
}
function changecolor ($Newcolour)
{
$this-> color = $Newcolour;
}
function colorappears ()
{
echo " The color of the car : " . $this->color;
}
}


$a = new Car();
$b = new Car();
echo "<b>The first car</b><br>";
$a->colorappears();
echo "<br>The first car changed colors<br>";
$a->changecolor("Blue");
$a->colorappears();
//
echo "<br><b>Second car</b><br>";
$b->changecolor("Green");
$b->colorappears();
?>

PHP Class Defined
php code

Save the php code with pbo01 name into htdocs folder. Run the program by opening the browser, then type http://localhost/pbo01.php in the address bar then enter. The result will look like this.
PHP Class Defined
Program view

Program 2
File Name: pbo02.php
Description: Program class to create a simple input form.
Type the following php code into notepad.
<?php
/*
Class Name: Form
Description: CLass to create simple text input form
*/

class Form
{
 var $fields = array();
 var $action;
 var $submit = "Submit Form";
 var $Fieldcount = 0;

 function __construct($action, $submit)
{
 $this->action = $action;
 $this->submit = $submit;
}
function displayForm()
{
  echo "<form action='".$this->action."' method='POST'>";
  echo "<table width='100%'>";
  for ($j=0; $j<count($this->fields); $j++) {
   echo "<tr><td align='right'>".$this->  fields[$j]['label']."</td>";
   echo "<td><input type='text' name='".$this-> fields[$j]['name']."'></td></tr>";
  }
echo "<tr><tdcolspan='2'>";
echo "<input type='submit' value='".$this->submit. "'></td></tr>";
echo "</table>";
}
function addField($name, $label)
{
 $this->fields [$this->Fieldcount]['name'] = $name;
 $this->fields [$this->Fieldcount]['label'] = $label;
 $this->Fieldcount ++;
}
}
?>

PHP Class Defined
php code

Save the php code into the htdocs folder and name it pbo02.php
Program 3
File Name: pbo03.php
Description: Program utilizes Program 2 to create simple input form.
Type the following php code and html code into notepad.
<?php
include "pbo02.php";
echo "<html><head><title>College student</title></head><body>";
$form = new Form ("","Input Form");
$form->addField ("txtsin", "Student ID Number");
$form->addField ("txtname", "Name");
$form->addField ("txtaddress", "Address");
echo "<h3>Please fill out the following form :</h3>";
$form->displayForm();
echo "</body></html>";
?>

PHP Class Defined
Php code and html code

Save the code with pbo03.php name in htdocs folder.
Program Explanation 3
Program 3 above is an example of a class call that is defined in Program 2 (pbo02.php). For the first time the pbo02.php program should be included using the include() function. Pay attention to the 2nd line program. To add the form inputan text box, just need to call the addField() function.
Run the program: open the browser, type http://localhost/pbo03.php in the address bar, then enter. Display results from the above program can be seen in the picture below.
PHP Class Defined
Program view

That's my explanation, about PHP class defined. Hopefully the explanation can increase our knowledge in learning php programming language.

Comments