Create HTML form with PHP Class ( Reposted from tisuchi.com)

This section would be very simple task for OOP with PHP developer. Although it could be very good foundation for beginner of Object Oriented Programming in PHP. In this tutorial, I am going to explain how to create HTML form by using PHP class. Its a very straight forward class that will generate few HTML form elements by calling particular function. Let’s see…

 Download from Github Demo

For implementing this tutorial, I am using 2 separate files of PHP. One is specifically dedicated for declaring class and another is representing the object of that class. There is nothing fancy in this tutorial. Its a very clean HTML form example.

To know more, visit 

Create HTML form with PHP Class

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.

Form Data with OO PHP

Helo Friends , How are you?

I am ready to inform you how to use form data using PHP Class.

First of all, make a class named “myData”. Then follow the code here-

<?php
class myData{
    public $name;
    public $age;
    public $sex;

    public function setData($x, $y, $z)
    {
        $this->name = $x;
        $this->age = $y;
        $this->sex= $z;
    }

}

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<?php

    if($_POST['submit'])
    {
        $mName=$_POST['myname'];
        $mAge=$_POST['myage'];
        $mSex=$_POST['mysex'];

        //creating object now
        $data= new myData();

        $data->setData($mName,$mAge,$mSex);

        echo "Hello ".$data->name.". Your are ".$data->age." years old. You are a ";
        if($data->sex==1)
        {
            echo "Male.";
        }
        else
        {
            echo "Female.";
        }

    }

?>

    <form name="" method="post">
        Name: <input type="text" name="myname" required 
placeholder="i.e. Mr. Suchi"/><br/>
        Age: <input type="text" name="myage" required 
placeholder="i.e. 22" /><br/>
        You are: <input type="radio" name="mysex" value="1" /> 
Male <input type="radio" name="mysex" value="2" />Female<br/>

        <input type="submit" name="submit" value="Show My Data"/>

    </form>

</body>
</html>

Here is the code to access form data with object oriented PHP class.

Thanks for reading

Write your first Class on PHP

Hello friends. Hope you already learn very strongly about the object oriented programming and different between class and object. Now, I want to tell details about the class. The basic syntax of PHP Class is –

class CLASS_NAME {

private

// Some properties and method

public

// some properties and methods

}

OK. This is the basic syntax of class. Lets to describe.

1. Every class start with class key word before class name

2. class name is like a variable. We can choose anything for class name

3. we can use some property and method in both private and public.

Now, lets make our first class on my address.

class myAddress{

public

$name=”Bismillahir Rahmanir Rahim”;

function get_name()

{

return $this->$name.'<br>’;

}

function set_name($newValu)

{

$this->name=$newValue;

}

}

$obj=new myAddress;

echo $obj->get_name();

$obj->set_name(“Allah is Almighty”);

echo $obj->get_name();

$obj->set_name(“Muhammad (SM) is the messenger and Prophet of Allah”);

echo $obj->get_name();

OK. Now save this file in the name of test.php  and run this file in your local server. You will see like this output Insallah.

Bismillahir Rahmanir Rahim

Allah is Almighty

Muhammad (SM) is the messenger and Prophet of Allah

Ok. It is the very basic class on PHP. Thanks for read this article.

Strongly understanding OBJECT and CLASS

Hello every one. Let’s go to understand very strongly about Object and Class.

Before you can get too deep into the finer points of OOP, a basic understanding of the differences between objects and classes is necessary. This section will go over the building blocks of classes, their different capabilities, and some of their uses.

There is a common confusion in OOP: seasoned developers start talking about objects and classes, and they appear to be interchangeable terms. This is not the case, however, though the difference can be tough to wrap your head around at first.

A class, for example, is like a Sketch for a house. It defines the shape of the house on paper, with relationships between the different parts of the house clearly defined and planned out, even though the house doesn’t exist.

An object, then, is like the Final House built according to that blueprint. The data stored in the object is like the wood, wires, and concrete that compose the house: without being assembled according to the blueprint, it’s just a pile of stuff. However, when it all comes together, it becomes an organized, useful house.

More than one object can be built from the same class at the same time, each one independent of the others. Continuing with our construction analogy, it’s similar to the way an entire subdivision can be built from the same blueprint: 150 different houses that all look the same but have different
families and decorations inside.

Thanks for reading.