Showing posts with label OOP in PHP. Show all posts
Showing posts with label OOP in PHP. Show all posts

Thursday, December 11, 2014

Object Oriented Programming in PHP

One of the most popular web scripting language now a days in PHP formerly known as Personal Home Page as a developer this programming language one of my favorite in building different types of application from website to business applications on wide variety on clients. In this article I would like to share with you my experience in writing object oriented programming in PHP.

At first learning how to program in object oriented is quiet focusing if you have trained in procedural programming also known as structured programming. In object oriented programming we start in declaring a class which acts as blue print of our object. In our class we declare public variable that we will use are attributes or properties of our object and then after that we create our methods also know as behavior that will carry out the actions of our class. In this example I wrote a a class math and then I declare two variables as public variable which means I can use this two variables outside of my class. In PHP 5 we use the keyword  __construct which allows us to pass parameters in our function solve as our method in our class. 

We later create an object that will inherit the attributes and methods of our class math I called our object product. As you observe I am also using a keyword $this this keyword in PHP act as a pointer to pointing the attributes we have in our class in this case our attributes are $val1 and $val2. After which I create a function to perform the multiplication of the two value that we pass in our function parameters. I called the function named solve() this function will not only perform multiplication operation but also display the computed result on our web page.

public function solve() {
$sum = ($this->val1 * $this->val2);
echo "The product of " .$this->val1. " and " 
    .$this->val2. " is " .$sum.".";
}


To create an object I used the follow commands   $product = new math(2,4);  and I also pass two values to be multiplied by our function. Take note we know that we create an object by using the new statement in PHP. After which we display the result in our web page by declaring the following statements below.

echo "<h1> <font color='green'>"; echo $product->solve(); echo "</h1> </font>";   


I hope this object oriented programming article that I wrote will give you some insights how to write object oriented program using PHP as your programming language. The benefits of learning object oriented programming in PHP is very significant you can make your code must easier to write, maintain and most important you can easy learn how to use PHP framework like Zend, CakePHP, CodeIgniter and Symfony because this frameworks are written in object oriented way.

If you have some questions about my works feel free to send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

People here in the Philippines who wish to contact me can reach me in my mobile numbers 09173084360 and 09993969756.


Sample Output of Our Program


Program Listing

<?php class math { public $val1, $val2; public function __construct($val1, $val2 ) 
{
$this->val1 = $val1; $this->val2 = $val2;
}
public function solve()
{ $sum = ($this->val1 * $this->val2);
echo "The product of " .$this->val1. " and " .$this->val2. " is " .$sum.".";
}
} $product = new math(2,4); echo "<h1> <font color='green'>"; echo $product->solve(); echo "</h1> </font>";
?>