Discussion Board : Introduction to object oriented programming concept on the PHP, especially class and inheritance.
Requirements: webserver package, already installed.

According Wikipedia.Com, Object Oriented Programming is paradigm to object-oriented programming. All data and functions within this paradigm is wrapped in a class or object.

Key in OOP is the object. Objects can be said as a collection of variables and functions (which in OOP, the variable referred to as properties, and functions called methods) that are collected in a single entity. Methods used to manipulate data in the object, the properties are part containing information about the data.

For this tutorial we will discuss two basic concepts in OOP, namely Class , and Inheritance .

Class.
Class is a representation of the object, inside the class methods and properties have . Immediately, we create a class (save with the name student.class.php , and put all the scripts in this tutorial in a folder at the same ).

<?php
class student {

public $name;
public $address;

/*constructor, is a function that will always be executed when an object diinstansiasi / created. If a class must perform a certain function before running the code, then this is where to put that particular function (usually to call the library). */

public function __construct(){
  echo 'Student's Data.<br />';
}

public function show_name {
echo 'Student's name is : '.$this->name.'<br/>';
}

public function show_address {
echo 'Student's address is : '.$this->address.'<br/>';
}

}
?>

Yup, you have created a class. For using that class, we need to create, or initialize an object from that class. Now, let’s try to make an object from Student class above.

<?php
$student1 = new student; //this create new object in the class student named object student1

//assigning data into properties
$student1->name = 'Alan';
$student1->address = 'Temari Street, 15';

//call the method
$student1->show_name();
$student1->show_address();

?>

That above script will produce something like this one,

Student’s Data
Student’s name is : Alan
Student’s address is : Temari Street, 15

Inheritance.
Inheritance is a capability for extending some class that will inherit ALL the parent class’s character. In this case, the class that will inherit is called child class.

<?php
include('student.class.php');

class newstudent extends student {
privat $register_number;

public function show_register_number () {
echo 'Student's Register Number is : '.$this->register_number.'<br/>';
}

}
?>

Can be explained that the object of new students (new student) must have different properties with the object pupil (student), where for the new students have a property register_number (registration number) that is not owned by the object pupil. Meanwhile, a new student object has all the characteristics possessed by the object pupil. This is where the concept of Inheritance useful. Instead of having to create a new class for new students object, we could extend it from an existing object, that object of students, by adding properties and methods specific to the new student object.

In this case,
object student (murid) –> parent class
object newstudent (murid baru) –> child class

For using child class, you can try to use this script below,

<?php
$student2 = new newstudent; //this create new object in the class student named object student1

//assigning data into properties
$student2->name = 'June';
$student2->address = 'Kebangkitan Street, 28';
$student2->register_number = '193-123-111';

//call the method
$student1->show_name();
$student1->show_register_number();
?>

That above script will produce something like this one,

Student’s Data
Student’s name is : June
Student’s address is : Kebangkitan Street, 28
Student’s register number is : 193-123-111

The Ends :D Hope that tutorial above could give basic concept for Object oriented programming with PHP, however, this tutorial -surely- still very poor for those who want to learn OOP.

Happy Coding :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

Tinggalkan Balasan

Alamat surel Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: