Skip to main content

Encapsulation in PHP

Encapsulation is wrapping some data in an object. It is about controlling the visibility of things and define something like the order of layers of our application.

Encapsulation in PHP OOP is about control and limit which parts of our application can access some class methods and properties using three keywords called in OOP world Access Control Modifiers , and these keywords are : public , private , protected  and this keyword comes in the beginning of declaring method or property just like below :

class Member {
  public $id;
  protected $username;
  private $password;
}

Public keyword modifier is used to define a variable or function as being visible to everyone, everywhere.  When this modifier is used the variable or function following is visible to the users using your class as well as all the inner workings of your class.  This modifier is the only modifier that allows interaction outside of the class.

Private keyword modifier is used to define a variable or function as being visible only to the inside of the class.  When this modifier is used the variable or function following is invisible to the users using your class, but all the code that you write inside your class will be able to see it.

Protected keyword modifier is similar to the Private keyword modifier in that it is visible inside of the class but not to users outside the class.  In addition to that however, the protected keyword also allows child classes that extend the class to see the characteristics as well. 

Comments

Popular posts from this blog

MySQL Connection

Connection with MySQL Database Before accessing database, you must create a connection to the database Syntax: mysql_connect(servername,username,password); where, servername specifies the server to connect to. Default value is “localhost” username specifies the username to log in with. Default value is the name of the user that owns the server process. To connect offline we use username “root”. password specifies the password to log in with. Default is “” Code : Creating connection to the database and selecting the required database <?php $con = mysql_connect(“localhost”,”root”,”"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } else{ mysql_select_db(“test”, $con) }; ?> Here, we have store connection in a variable called $con and trap error using die function. Closing connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_conne...

Type Juggling and Type Casting

Type Juggling: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

Doctype Defination

A Document Type Declaration, or DOCTYPE, is an instruction that associates a particular SGML or XML document (for example, a webpage) with a Document Type Definition (DTD). Syntax The general syntax for a document type declaration is: <!DOCTYPE root-element PUBLIC "FPI" ["URI"] [ <!-- internal subset declarations --> ]>