PHP: Coding on OOP Concept Part 1

By | March 23, 2016

PHP: Coding on OOP Concept Part 1

In this, Programming concept OOP , is very helpful and is a common format for application development in this period. It is designed to support the complex problem solve and another is that it can reusable code makes code development even faster and in PHP, it is support  in OOP concept. on this article part 1 show step to..

  1. create  class
  2. create member , method
  3. create object

Okay, let’s get started with. I will build a Class students about simple things.  Class Student has two member is  firstName and lastName and have one method  to display () with the following code.

</pre>
<pre><?
class Student{

public $firstName;
public $lastName;

public function display()
{
$display = 'FistName: ' . $this->firstName;
$display .= '<br/>';
$display .= 'LastName: ' . $this->lastName;
return $display;
}
}
</pre>
<pre>

Note that in the time to write a php class mostly he would be no end tag?>.

When we finished, we will write code a class to create an object class Student . By that I created by file name  “index.php” contains the following.

</pre>
<pre><?php
require 'class.Student.inc';
echo '<h2>Test Class Student</h2>';
$student = new Student();

echo '<h2>Set Property Class Student</h2>';
$student->firstName = 'Thaicoding';
$student->lastName = '.Com';
echo '<pre>' . print_r($student) . '</pre><br/>';

echo '<h2>Function Display</h2>';
echo $student->display();
?>
</pre>
<pre>

When I try to run it and see the results below.

PHP: Coding on OOP Concept Part 1

 

To see that we  create a new object, which same other languages programming  ​​supported OOP concept. Then later we have to be assign value to firstName and lastName which we can directly access because the member access specifiers is public. When we done to config in index.php file that is used to function print_r object student in the final run method display all value on object class Student. Student or member of the reference method in the PHP language is used -> symbol.

Download SourceCode

Leave a Reply

Your email address will not be published. Required fields are marked *