PHP OOP

PHP & MySQL Course:  PHP OOP

In the previous lecture 7, we learnt about the how we can use the HTML tags in PHP, and we use the

dot operator to concatenate the strings in PHP and we also discussed the display functions as they

are echo() and print().

Lecture 8 PHP OOP:

In this lecture you will learn about the most important topics, from which we use the OOP concepts in PHP.

The topics from php oop  are following:

  1. Creating Classes in PHP
  2. Declaring Objects in PHP
  3. Using constructions in PHP
  4. Using keywords with in class in PHP

Creating Classes in PHP:

First of all we should know about the class. The class concept comes from the link, I mean we use class when

we are targeting one thing in details. As if we create a class and name it student, so there are some attributes of the

student. Which are student name, student roll number, blab blab blab. Like this there are many things we can create

classes for them. By using the class our data is secure and we don’t know what is happening in the class during the

execution of the methods which are inside of the class. So we just know that for what purpose we can use the class.

For example people commonly using the internet they can access any website using the website name or address.

When they write the website address in the browsers address bar and press enter. Then what happen just nothing

the website comes in front of them. But they don’t know how it happens in the back end. In PHP programming we

use class keyword to declare the classes and then we use the class name to name it.

Syntax:

Class className{

Constractors…..

Methods……

Variables…..

Statements……

}

Listing 1: Class in PHP Programming

</pre>
<?

class Student{

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$Student1 = new Student;

$Student1->StudentInformation();

?>
<pre>

In the listing 1, I declared a class using the keyword class and used the Student as name. Inside the class there

is a method name StudentInformation which contains data about the students. And we called the

method using the $Student1 as an object. In the PHP declaring an object is little bit different as we usually

declare any object in Java and C++ object oriented programming languages oop. If you want to get information

about the object and class so use the var_dump() function. For details check the listing 2.

Listing 2: var_dump()

</pre>
<?

class Student{

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$Student1 = new Student;

$Student1->StudentInformation();

var_dump($Student1);

?>
<pre>

By the help of the built-in method var_dump(), we can check the contents of the class. This method shows the

details about the class when we execute the program on the browser.

As we can also use the public keyword in the class with any variable or method. By which the variable and

method will be easily access outside the class. We do this commonly with the variables. Lets we try

to use the public keyword with variables in PHP.

Listing 3: Using Public keyword in class

</pre>
<?

class Student{

public $display = "http://www.trustingeeks.com";

public function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->display;

?>
<pre>

We used the public keyword in listing 3, there is a variable and a method which both are public. Its mean that

these both can be used them outside the class easily. We can also access the methods of the class without using the

public keyword.

Constructors & Desctructors in php oop:

In PHP we declare or define constructors in a different way. As usually we declare or define the methods in

Java and C++ by the same name as the class have. But in PHP programming we use a keyword as

__construct(). And we don’t need to call it by self. When we declare any object in PHP programming for the class,

at that time the __construct() method called and executes. Lets try it in listing 4 for details.

Listing 4: Constructors in PHP Programming

</pre>
<?

class Student{

public function __construct(){

echo "The class is called without any trouble...<br>";

}

function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->StudentInformation();

?>
<pre>

As you can see in the listing 4, declaring on object we called two methods. As the constructor is also a method. Now

we will destroy the constructor using the __destruct() keyword. When we will declare an object so it will also destroy

the constructor. Let’s check the listing 5 for details in which we will also check the class name by using the keyword

__CLASS__, this keyword will tell us about the name of the class.

Listing 5: Destructors in PHP

</pre>
<?

class Student{

public function __construct(){

echo 'The class "',__CLASS__,'" constructor is created...<br>';

}

public function __destruct(){

echo 'The class "',__CLASS__,'" constructor is destroyed...<br>';

}

function StudentInformation()

{

$name = "Ashley";

$rollnumber = 12344;

echo "Name of the student is ".$name;

echo "Student Roll Number is ".$rollnumber;

}

}

$print = new Student;

echo $print->StudentInformation();
<pre>echo "<br>";
?>

Summary:

In this lecture we started the object oriented programming in PHP. The concept of the OOP should be clear

in details.Because if the programmers don’t know about the OOP in details or their concepts are not clear so it

will be difficult for them to develop any website as a dynamic.

In this lecture we learnt about the class and its objects. Using the class we use the keywords as there are: public,

__construct() and __destruct() these keywords are very important to be known for a programmer. Because

the OOP concept of the PHP depends on them. And we also used the __CLASS__ keyword to take information

about the class as its name.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development