Understanding Interface in Object Oriented PHP (Published in tisuchi.com)

You may heard about Interface in Object Oriented PHP Programming. But have you ever asked yourself that, what exactly interface? How it work? Or what is the functionality? As a php developer, why you have to bother about it?

DOWNLOAD SOURCE FILES FROM GITHUB

Well, if you have that kinds of questions in you mind, you may be lucky that this tutorial is for you. This writing is totally dedicated to understand Interface in OO PHP. What exactly interface do? Lets see…

 

What is Interface?

An interface is a contract between unrelated objects to perform a common function. Literally it guarantor for users to ensure that it is capable to perform all the functions of object that is defined specification. For example, cricket bat and football are totally different things in terms of shape and size, but in object representing those items in a sporting goods store inventory must be capable of interacting with that system.

It is very important to understand that, by declaring an interface and then implementing it in objects, can be hand completely different classed to common functions.

 

To know more, visit http://tisuchi.com/understanding-interface-object-oriented-php/

 

Advertisements

Part 1: OOP by PHP: From Novice to Pro

This is a series of Object Oriented Programming based on PHP. Today I am going to show you the 1st part of this series. OOP in PHP was never been easy those comes from procedural method. This series will be delivered very smoothly that programmer can understand those comes from procedural practice. Hopefully you will enjoy it.

Thanks for watching.

PHP-MySql Data Insert by OOPHP pattern

Hello Friends, Now we are going to learn how to insert data into mysql database by using object oriented php pattern. For this, I am making a database table called ‘datainsert’ and database name should be oophplearn.

Then we are going to write our first php function for Object Oriented in the file of ‘functions.class.php’. The code are in below-

<?php

class dataInsert{

public $command;

public function __construct(){
@mysql_connect (‘localhost’,’root’,”) or die(mysql_error());
@mysql_select_db(‘oophplearn’) or die(mysql_error());
}

public function insertData($value){

$sql = “INSERT INTO datainsert VALUES(”, ‘”.$value.”‘)”;

$this->command = @mysql_query($sql);

if($this->command){
echo ‘Data Insert Successfully…’;
}

}//public function insertData($value){

} //class dataInsert{

?>

Now I am writing another file called index.php into our directory. The code are in below-

<?php
//including the functions
require_once(‘functions.class.php’);

$fun = new dataInsert();

//confiming work if submit button will be press…
if(isset($_POST[‘submit’])){

//calling the function from class…
$fun->insertData($_POST[‘name’]);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Data Insert by Object Oriented PHP</title>
</head>

<body>
<form name=”” action=”” method=”post”>
Name: <input type=”text” name=”name” id=”name” placeholder=”Type You Name” required/>
<input type=”submit” name=”submit” value=”Save My Name”/>
</form>
</body>
</html>

Now, save all the works and run the index.php file by browser. Ok, if everything is okay, then you will get a from to write you name. Just write your name and submit your name. Then you will get a notification for confirmation of your post. Ok, now check in your database, and you will get your new information into your database. That’s enough for today.

Keep in touch and bye for today.

PHP-MySql Hit Counter by OO PHP Pattern

Hello Friends, How are you?
Today I am going to learn how to make a Web Page Hit Counter by using PHP and MySql with Object Oriented PHP Pattern. For this work, we make only 2 files of PHP and 1 MySql Database with only one table. Lets go to start…

First of all, I am making a MySql database table for this. The database name should be ‘oophplearn‘ and table name should be ‘hitcounter‘. The structure of the table is below-

id count
 int (11)- Auto Increment  int(11)

Ok, now we are going to write our code. First of all, I am opening the notepad++ and create a new php file called ‘functions.class.php‘ . The code of this page is-

 

<?php

class Hitcounter{

//declaring the variables
public $command;

Continue reading

PHP Mysql Connection by OOPHP pattern

Hello Friends.  Hope you are fine. Now, I am going to show, how to connect PHP and MySql by using Object Oriented PHP pattern. First of all, creating a new php file by using dreamweaver. Then I am calling a class called database. Then declare the necessary variables. Then Code are below-

class database{
                          //defining the variables
                          public $db_host;
                          public $db_user;
                          public $db_pass;
                          public $db_database;
}

Now, I am creating the functions. First of all, I am creating the function for database connection called function db_connect(). Then I am declaring the necessary functions. The code are below-

function db_connect(){
                         $connect = @mysql_connect($this->db_host, $this->db_user, $this->db_pass);
if(!$connect){
                         @mysql_close($connect);
                         die(‘<h1><font color=”#ff0″>Error! MySql Connection Failed!</font></h1>’);
                         return flase;
}
                         return true;
}

Now, I am creating another necessary function for choosing the select database. The Code are in below-

function db_select(){
                         $select = @mysql_select_db($this->db_database);
 if(!$select){
                         @mysql_close($connect);
                         die (“<h1><font color=’#000000′><center>Not <font color=’#FF0000’style=’text-decoration:blink;’>Selected</font> DataBase !</center></font></h1>”);
                         return false;
}
                         return true;
}

Continue reading

Extending a Class in OOPHP

Hello Readers

Lest go to check, how to extend a class in Object Oriented PHP. It’ s very easy to make. One of the greatest features in OOP is that you can extend a class and create a completely new object. The new object can retain all the functionality of the parent object from which it is extended or can override. The new object can also introduce some features. Let’s extend our myClass class and override the myNewClass function so that it can send HTML mails.

First of all, create a file named class.extend.php and write these code into this file.

<?php
class myClass{
 public $name;
 public $address;
 
 public function myName($name, $address){
 echo $this->name = $name."<br>";
 echo $this->address=$address."<br>";
 } 
}

class myNewClass extends myClass {
 private $age;
 
 public function myProfile($a, $b, $age)
 {
 $this->myName($a, $b);
 echo $this->age=$age;
 
 }
}

 

//creating a object 
$user= new myNewClass();
$user->myProfile('Suchi', 'Uttara', 23);
?>

 

 

Thanks to read. Take Care.

Word Count in a file by Object Oriented PHP

Hello Readers
Now I am ready to show you, how to make a object oriented PHP program to count the words in a file. Let’s go to start the work.
First of all, you have to create two files called index.php and mywords.php. In index.php file, you have to write the below codes.

<?php

//creating a class
class WordCounter{
const ASC=1;        //this is a constant
const DESC=2;    //this is a constant

private $words;   //called a private variable

function __construct($filename)
{
$file_content=file_get_contents($filename);
$this->words=array_count_values(str_word_count(strtolower($file_content),1));

}

public function count($order)
{
if($order== self::ASC)
{
asort($this->words);
}
else if($order==self::DESC)
{
arsort($this->words);
}

foreach($this->words as $key=>$val)
{
echo $key.” =”. $val.”<br>”;
}
}
}
?>
<!DOCTYPE>
<html>

<body>
<?php
$wc=new WordCounter(“myWords.php”);
$wc->count(WordCounter::DESC);

?>
</body>
</html>

Now, go to create a another file named ‘myWords.php‘.
<!DOCTYPE>
<html>
<head>
<title></title>
</head>

<body>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

</body>
</html>

 

Now, run your file in your localhost / web server to show the result.

 

Thanks for reading.