A tutorial on private and public differences in PHP OOP

Source: Internet
Author: User

On the previous page I began to explain the importance of object-oriented and how to create a basic object. We will finalize a lot of details here and now.

class variables and visibility
In class, we first determine our variables. In this case, our first variable is the $ name. The convenient feature of PHP5 (not PHP4) is to increase visibility control. Our $ name becomes privatized with current visibility. Private visibility means that the variable can only be passed through the internal methods of the class (such as SetName and GetName). If we set the variable to visible citizens, our variables can be accessed externally (excluding methods). The final visibility settings are protected by a variable, which means that variable access is limited to parents and inheriting classes that we will discuss later.

In PHP4, the public is the only choice for popularity and public variables declaring the use of reactive power rather than the public. Let's look at how the visibility works and increase the $ position variable set for public visibility:

/* Instantiate Object * *
$kirupa = new User;

/* Access private variables via methods, then try without methods * *
$kirupa->setname (' Kirupa ');
echo $kirupa->getname (). ' <br/> '; Kirupa
echo $kirupa->name. '  <br/> '; Breaks our code

/* Access public variables without methods'll work * *
$kirupa->location = ' MIT ';
echo $kirupa->location. ' <br/> '; MIT

Class User {
 private $name;   //must is accessed with methods.
    //Not a vailable in PHP4
      
 public $location;  //can is accessed without Methods
    //var $location; in PHP4
 
 function setname ($val) {
&NBSP;&N BSP; $this->name = $val;
  return;
 }
 
 function getName () {
  return $this->name;
 }
}

So the visibility of the variable which one should you choose? The answer is, it depends. In general, you should use private fines. Private enterprise is particularly useful because it supports encapsulation-the ability to hide data only so that it can pass through specific interfaces. In Object-oriented, an interface represents the functionality given to a particular object.

As we set a variable of popularity, we can also set the visibility of the method. By default, if we do not establish the visibility of our methods, they will be set to public. For the sake of time, we will not delve into the examples of different methods of popularity. Continue to understand an important and useful method of structuring.

Note: This is: In terms of methods, we refer to the level variable and use the other method $ this-". This can be seen in the SetName and GetName methods. $ this is a preset variable when an instance is created to make an object reference itself.

Note the naming convention: Although the establishment of [PropertyName] and
Get the [PropertyName] popular approach because their names are easy to understand and they don't need them. You can name these methods whatever you want.

Construction method
Now is a good time to look at the construction method. The construct is a method that will automatically be run-time object instances. In PHP5, our approach is called "Magic Method" (ie __construct, __destruct, and others), which are from two bottom lines. In PHP4, the construction method will get the same name as the class itself. Therefore, in PHP4, if we have a class named user, our construction method will also be named by the user (not __construct). Remember that you do not have to call the construction method explicit because it will automatically run an instance when the class is established. Here is an action in the structure:

$kirupa = new User;
echo $kirupa->getjob (). ' <br/> '; Engineer

Class User {
Private $job;

function __construct () {//function User () in PHP4
$this->job = ' Engineer ';
}

/* Job Methods * *
function Getjob () {
return $this->job;
}
}

Now you may start to see one of the benefits of the object-many of the code is in the background. We can include files that contain our class hidden heavy code even more. Since the naming convention classes and methods are fairly self explanatory, we will not need to constantly review our inclusion in the archive. Read on and learn how we can use arrays to speed up our process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.