Translated by Google Translate

Public, Private, and Protected (Member Visibility) in OOP PHP



In OOP we will always find keywords Public, Private, and Protected. These three things are used to determine the visibility of members (properties and functions) that are characteristic of object-oriented programming. Are members globally accessible (public) or accessed by its class (private) or accessed by the class and subclasses. Meanwhile, if the procedural programming members can only be accessed globally.

A. Public

In public visibility, a method or property can be accessed globally. That means it can be accessed on the inside and outside of class or in other words the properties and functions are known globally. In OOP PHP tutorial for Beginers previously, we have made $objPeople which the visibility of properties and function are public.

This is another example of class with public visibility :

Script 1.
<?php
class student {
    public $sid;
    public $name;
    public $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

And this is the result looks like in browser
Picture 1. Public member visibility

B. Private

If public can accessed by global  then private visiblity means that the member (method or property) in a class are accessible only within its own class.

Let's modify the script at class student by changing the property visibility to be private.

Script 2
<?php
class student {
    private $sid;
   
private $name;
   
private $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

If we run the script 2, it will display an error message because the property sid, name and points have private visibility, so that the data accessed by outside of class can not be done. So how do the private property that is accessible outside of class so that it can be used by objects that need it? The solution is to use the function setter and getter.

Setter is used to create a (set of) value for a property. This function is generally used for properties that have private and protected visibility. Setter is often used to validate incoming data before the data is loaded on a property. While the getter is used to generate (get) a value of the member public, private, or protected from the result.

How to access private member with that function?

Script 3
<?php
class student {
    private $sid;
    private $name;
    private $point;

    function setSid($x) {
        $this->sid=$x;
    }
    function setName($x) {
        $this->name=$x;
    }
    function setPoint($x) {
        $this->point=$x;
    }
    function getSid() {
        return $this->sid;
    }
    function getName() {
        return $this->name;
    }
    function getPoint() {
        return $this->point;
    }

    function Point () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->setSid('3310901051');
$objStudent->setName('Hermanto');
$objStudent->setPoint(90);
$objStudent->point();
?>

In the script above, we use function setters and getters for each property that has a private visibility. Making the value of the function by taking the value setter and getter done by function. In the getter is used "return" value because the getter will return the value of the property taken by the data.

Note how to calling the property after instancing $objStudent, calling the property is done by calling the names function with ($objStudent-> setSid()). Although become longer code lines, but more OOP recommend this method for accessing a Member.

If the script is run in the browser then the result will be the same as the picture below :
Picture 2. Calling private visibility with setter & getter function

C. Protected

Protected almost SAME to the private that can only be accessed by its class and protected can also be accessed by its subclasses. Let's try changing the script 1 and we will change the visibility of the member be protected like the script below :

Script 4
<?php
class student {
    protected $sid;
   
protected $name;
   
protected $point;

    public function processPoint () {
    echo "Students with sid $this->sid <br />";
    echo "name $this->name <br />";
    echo "get point $this->point";
    }
}
$objStudent = new student();
$objStudent->sid = '3310901051';
$objStudent->name = 'Hermanto';
$objStudent->point = 90;
$objStudent->processPoint();
?>

If the script is run in browser then the result will error : cannot access protected property.

The error occurred because the protected class can only be accessed by its class and subclasses. For more detailed discussion of inheritance protected in subclasses will be explained on the nexy tutorials on The Inheritance.


Read More Add your Comment


What is AJAX and How to Use it With jQuery



Are you questioning What is AJAX, and How to Use it With jQuery? To answer the long question, we need to start it from the meaning of AJAX. No, it is not football club from Holland but AJAX is asynchronous JavaScript and XML. People use AJAX to load data from a server and then display it on the webpage without the need to reload the page. With AJAX, you will see only part of the website is changed while the other remain the same because the page is not refreshed. If you still confuse, you can open your Gmail and switch from “inbox” to “sent mail”. You will see the content is different but the page is the same. AJAX call is something you do behind the scene.

So, what is the benefit of AJAX?  
Minimize Bandwidth Usage
AJAX can work without reloading page, so you don't need to refresh and excedded your bandwidth usage.
Faster
Facebook is one from many website that using AJAX. When new notifications coming, it will show you the alarm or notification info in right top position of your facebook page, that's work by AJAX. You can imagine how it works without AJAX, you don't know if there are new notifications coming, you should refresh your facebook page to know that.
- User Friendly
You don't need to refresh the page if you want to see the changes.

Now that you know what AJAX is, we are half way. To complete the answer for What is AJAX, and How to Use it With jQuery we need to continue with it application with jQuery. If you wonder why jQuery, people love it because jQuery supports AJAX in fantastic way. Even more, you will find the implementation is very easy. One important thing is to tell the browser whether to catche AJAX or not, especially since different browsers will behave in different ways.

Here is how to do it :
$.ajaxSetup ({
cache: false
});
This is the example :
$.ajaxSetup ({
cache: false
});
var ajax_load = "<img src='img/load.gif' alt='loading...' />";
//  load() functions
var loadUrl = "ajax/load.php";
$("#load_basic").click(function(){
$("#result").html(ajax_load).load(loadUrl);
});
Example of $.ajax with success is:
$.ajax({url:'/'}).done(function(data){});
Now you have the answer for What is AJAX, and How to Use it With jQuery. This is the time to apply it and see what happen.


Read More Add your Comment


HTML Basic Part 2



Meet Again ! as Usual This is R1O as the guest Author. I will continue explain the part from the first tutorial in this HTML Series. If you have not yet read the first tutorial i suggest you to read the first one otherwise you will get lost. Trust me ...
So far we have write this code in our text editor
<!doctype>
<html>
</html>
And the last discussion we talk about tag. So now just add the <head> tag opening and closing in the <html> area in your text editor. Like the example below.
<!doctype>
<html>
<head>
</head>
</html>
Now, What is the tag <head> is use for ?
The tag <head> is use for processing advance programming stuff like javascript, PHP, and etc..
Just remember this “ all the cool stuff that you look at the website is processing at here “ the <head> tag “ “.
Allright now we gonna add <title> tag. write this code below on your text editor. And save it. Remember the format of the save file is .html
<!doctype>
<html>
<head>
<title>

</title>

</head>
</html>
<title> tag what’s that?
The <title> tag is use for make title or description on the browser tab.
So now try add the title or description. Type this code on your text editor and save it.
<!doctype>
<html>
<head>
<title>
                My site
</title>

</head>
</html>
It should look like this in your web browser , in this case i use internet explorer:

TITLE TAG

Now your website has a title ! yeah ...
you can change the name as you like this is only the example.


Now let’s jump to the <body> tag. Write the code as seen on below:
<!doctype>
<html>
<head>
<title>
                My site
</title>
<body>
</body>

</head>
</html>

<body> tag ?
<body> tag is use for make the appereance in the web. If you see the website paragraph or picture it’s written this section the <body> tag. I show you the picture so you can get the clearer picture:

BODY TAG

You see the white space is called the <body> area. Where the upper left is starting point to positioning things in the website. Is like the cartesius diagram that we learn from school in mathematic subject. The difference is the starting point instead from the middle in website is start from the upper left.

BODY TAG
That’s for today. We gonna cover how to add somes stuff in  the <body> tag in the next tutorial. So stay tune !


Read More Add your Comment


HTML Basic Part 1



HTML BASIC
 Yo folks back again this time with me as a guest writer R1O. Today i’m Gonna Share about how to design a website From the very basic. Do you ever wonder what website is consist of? Well no need to wonder again cause i’m gonna tell ya. Website Is consist of HTML, CSS, JAVASCRIPT, PHP or even you want to make more dynamic you can add Flash and HTML5 Element. This time I’m just want to share about HTML because that i’ve learn so far.

What the heck is HTML ?

HTML Stand for Hyper Text Markup Language. This is the language that computer understand and this is also the backbone of a website.

How Do i Start writing the HTML ?

You can code it on text editor for example : notepad++, Sublime, or the very basic and stunningly beautiful is the notepad from Windows. (belum di link)
Download It first Just click on the example i suggest using notepad++ and install in your damn PC

Ok, what’s next?

Type exactly same as below. .  .
<!doctype>
<html>
</html>
Just Type like that i explain later... and save as .html for example: index.html (you can add the as you like, index html is the common one.

Ok, now what the heck is <!doctype> and <html> </html> ?

The code <!doctype> is the declaration Tag statement and it must be at the top of the HTML File. It means you tell the browser that this is for code in HTML or HTML5. The <html> is the opening tag and the </html> is closing tag. This is the rule of making HTML you must type that two things first.

What is tag?

In HTML language Tag is the code and it wrap the word in <> for example <head> and tag <head> is the opening tag and it have closing tag to it looks like this </head>. The closing tag has the / before the word. Note not all the tag have closing tag for example the <br /> it close on just one line. I explain the tag that i just wrote later in the next tutorial.



Read More Add your Comment 0 comments


 

©2013 @namakuherman | Developed by Herman Creative Industry