Translated by Google Translate

OOP Concept in PHP for Beginner




Object oriented programming has begun in era of PHP3, but still simple and not perfect. While web technologies and cloud computing grows up then PHP 5 has been developed to support full OOP. OOP become popular among web programmers. For example, many CMS like Joomla, WordPress, PrestaShop and Moodle are built with OOP. Furthermore, trend use of Frameworks like Yii Framework, Codelgniter, Zend, CakePHP and etc. All of them also built with OOP.

So, how to implement OOP in PHP? To answer this question, first you must have mindset when using OOP "Everything is an Object". Object is the "main actor" in OOP. So when you developing a website in PHP you should thinking about Object. You should think how interaction between obejcts can solve the problem.

Now, let's  implementing OOP in PHP, There are four basic terms you should be known in OOP. They are Object, Class, Property and Method. And here they are :

A. Class

A class is a blueprint of a template according to which objects are created. Class is bearing of group element data (variable) and code program (function). Variable in class called property and function is method. To create a class in PHP, you can use the keyword "class" and then followed by class name. For an example here a class with name "People".
<?php
class people {
public $name;
public $age;
}
?>

In code above we create a class with name people (red) and its properties, name and age (green)

B. Method

Method in OOP can be defined as behavior or anything what can be done by object from a class. In PHP, method in class realized in function. Method can be called  as anything what can be done by object. For an example, object people could have speaking, walking and agestatus. Here's the example of implementing method in class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
?>
in code above we create function named speaking and with content name and age.

C. Instance Object

Object is instance of class, so a class can't be used without object. To create an object in PHP we can use tag new and the code format is like this

$objectName = new className()

Look the code below, we will create an object for class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
?>

In code above we create an object instance from class people name $objpeople. When run in browser, it view nothing that's because the script not contain a command to doing something at object $objpeople.

D. Set Properties

After success instancing an object, now we'll setting the properties in object. In this case we'll setting the properties name and age. To setting the properties we can use : 
$objectName->property = value;
Let's implement this to object people :
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
//VIEW THE PROPERTIES VALUE DI RROWSER
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age years old";
?>

At $objPeople->name = 'Herman'; We set the name value with "Herman" and $objPeople->age= '23'; We set the age value with "23". And to run that properties we use :
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age Tahun";

And this is the code looks like in browser :

Picure 1. Set Properties

E. Running the Function

Here we try to call the function in PHP. In the previous case we create a funciton name speaking. Here's how to call that function.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
$objPeople->speaking();
?>

And how it's look like in browser:

Picture 2. Call the Function
To call the function we can use $objectName + "->" + functionName() + ";".

And that's all, thanks for coming, wait my next PHP tutorial.


Read More Add your Comment


PHP for Beginners, Fast and Simple Learning



To start learning PHP, you should have basic knowladge of HTML. There are 2 ways to create PHP file, which embadded script and non embadded script. Embadded script is PHP that placed in HTML script. While non embadded script is PHP script wrote without placed in HTML script. Here's the difference between embadded script and non embadded script when displaying string using echo command.

Embedded Script
<html>
<head>
<title>WebDev777.COM - Web Development Research and Tutorial</title>
</head>
<body>
<?php 
echo "WebDev777.COM - Web Development Research and Tutorial"
?>
</body>
</html>

Non Embadded Script
<?php 
echo "WebDev777.COM - Web Development Research and Tutorial"
?>

Or
<?php
echo "<html>";
echo "<head>";
echo "<title>";
echo "WebDev777.COM - Web Development Research and Tutorial";
echo "</title>";
echo "</head>"; echo "<body>";
echo "WebDev777.COM - Web Development Research and Tutorial";
echo "</body>";
echo "</html>";
?>

Or
<html>
<head>
<title>WebDev777.COM - Web Development Research and Tutorial</title>
</head>
<body>
</body>
</html>

<?php
echo "WebDev777.COM - Web Development Research and Tutorial"
?>

Explanation
In embedded script, the code placed in anywhere in html script, whereas non embedded script is php script wihout html.

After knowing how to create php file, now we go to the main lesson, how to send data via POST and GET parameters. PHP has 2 method that can be used for send data from client (browser) to the server, and that they are, POST and GET.

POST method is used to transmit data from client to server where the data will sent via http header. For an example we create an input form with name forminput.php. The script is below :

<html>
<head>
<title>WebDev777.COM - Web Development Research and Tutorial</title>
</head>
<body>
<form section method="POST" action="tInput.php">
Title : <input type="text" name="title"> <br>
Editor : <input type="text" name="editor"> <br>
Publisher : <input type="text" name="publisher"> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>

And this is the forms looks in browser
Picture 1. forminput.php
The script with red color using the post method to transmit data of title, editor, and publisher to tinput.php. (action = destination) that will using $_POST command to get the data sent. Because the parameters will be sent to tinput.php then we must create tinput.php.

tinput.php
<?php
$title = $_POST['title'];
$editor = $_POST['editor'];
$publisher = $_POST['publisher'];
echo "Title : <b>$title</b> <br>";
echo "Editor : <b>$editor</b> <br>";
echo "Publisher : <b>$publisher</b> <br>";
?>
 That red script used to get data that sent by client. And this is the script above looks in browser :
Picture 2 tinput.php
GET method that works with $_GET funcion will send data or communicate via parameter that wll be visible in the URL. For more detail, change the script in the forminput.php from [Method="POST"] to [Method="GET"]. And then change the script in tinput.php too, from $_POST to be $_GET. Now refresh your browser and click the submit button, the result will look like Picture 3 below.
Picture 3. forminput2.php

And that's all, thanks for coming ...


Read More Add your Comment 1 comments


HTML for Beginners, Knowing Forms (Web Design)




In the recent web design tutorial we have learnt first HTML How To Make Table and now we will learn about Forms in HTML. What is form?? Maybe you ever seen a form in real life, such at psycho test or collage registration. And for example look at a simple psycho test form below.
Picture 1. Registration Form
The registration form in picture 2 is use for collecting data from the registers. Same as picture 2 the form in website is use for collecting data that inputed from form. This is closely related with PHP and Database. So, because the function of form is use for inputing data then to create an HTML form we can use <input> tag.
<form>
input elements
</form>
In picture 2 there are option to identify the participant, like Mr., Mrs., or Ms. and the value for inputed is not charracter but check symbol. In HTML we can use checkboxes element. There are many elements in HTML form, the most common input types are described below :

Text Fields
Text Field is the most frequently used elements.
<form>
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname">
</form>
And this the HTML code above looks in browser:

First name:
Last name:

Password Field
<input type="password"> defines a password field:
<form>
Password: <input type="password" name="pwd">
</form>
And how it looks like in browser
Password:
The character in passfield are masked (shown as asterisks or circle)

Radio Buttons
<input type="radio"> defines a radio button. Radio buttons let a user select ONLY ONE of a limited number of choices:
<form>
<input type="radio" name="option" value="Yes">Yes<br>
<input type="radio" name="option" value="No">No
</form>
And how it looks like in browser
Yes
No
Checkboxes
<input type="checkbox"> Let a user choose more then on option.
<form>
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car 
</form>
And how it looks like in browser
I have a bike
I have a car
Submit Button
Submit button is used to send data to the server, data sent to the specified form action
<form name="input" action="http://apps.webdev777.com/code/sentmail.php" method="get">Email: <input type="text" name="user"><input type="submit" value="Submit"></form>
How it looks like in browser
Email:

If you type your email in the field above and click "Submit" button, you will receive an email from herman@webdev777.com


Read More Add your Comment


HTML for Beginners, How Table Make Layout (Web Design)




Many people says that HTML stands for Hyper Text Markup Language. But I have my own stands for HTML. In my experience when I begn autodidactiong HTML I learn how to make the layout. And I find the simple way to design the layout is using table tags. If you looking the web layout without fill (color and picture), you will got it. Look at the table below :

Table 1.  6 colomn and 8 rows
1/A B C D E F
2
3
4
5
6
7
8


The picture 1 shows the simple table that has 6 colomns and 8 rows. Let's make a little changing. Look at the table 2 below.

Table 2. Modify from Table 1
HEADER
LEFT SIDEBAR

In table 2, I merge all cells in the first row, so I name it Header. And then I merge 2 colomn (A, B) from second row until last row (2 - 8) and I name it Left Sidebar. After here we have build a simple layout from changing the table.

Now we try fill that table with color. Look at the table 3 below for the result

Table 3. Fill the color the table 2
HEADER
LEFT SIDEBAR
In Header I fill that with Rubber Ducky Yellow color, and In the Left Sidebar I fill that with Yellow. After here we have built a simple HTML Website. Try to modified this table with your own style. For source code you can copy the code below:

<table>
<tbody>
<tr>
<td><table align="center" border="1">
<tbody>
<tr>
<td height="40" style="text-align: center;" width="70">1/A</td>
<td height="40" style="text-align: center;" width="70">B</td>
<td height="40" style="text-align: center;" width="70">C</td>
<td height="40" style="text-align: center;" width="70">D</td>
<td height="40" style="text-align: center;" width="70">E</td>
<td height="40" style="text-align: center;" width="70">F</td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">2</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"><br /></td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">3</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">4</td>
<td height="40" width="70"><br /></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">5</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">6</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" style="text-align: center;" width="70">7</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"><div style="text-align: center;">
8</div>
</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"><br /></td></tr>
</tbody></table>
Code 1 for table 1

<table align="center" border="1">
<tbody>
<tr>
<td colspan="7" height="40" style="text-align: center;">HEADER</td>
</tr>
<tr>
<td colspan="2" rowspan="7" style="text-align: center;" width="140">LEFT SIDEBAR</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
</tbody></table>
Code 2 for table 2


<table align="center" border="1">
<tbody>
<tr>
<td bgcolor="#ffd801" colspan="7" height="40" style="text-align: center;">HEADER</td>
</tr>
<tr>
<td bgcolor="ffff00" colspan="2" rowspan="7" style="text-align: center;" width="140">LEFT SIDEBAR</td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
<tr>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
<td height="40" width="70"></td>
</tr>
</tbody></table>
Code 3 for table 3


Read More Add your Comment


 

©2013 @namakuherman | Developed by Herman Creative Industry