Declaring variable and method in PHP class


We knew  that variable is an object state. We need to declare all object state within PHP class.  We want to declare a variable within class such this way

class Myclass{
public $variable;
}

Add string in that variable

class Myclass{
public $variable= “Storing Variable Data”;
}

Declaring Method::

A method is an object behavior. Object behavior defines object task. So we want to print the variable data in PHP method. To do this at first write function then write method name with a pair of parenthesis () and body with curly braces {}
class Myclass{
public $variable=”Storing variable data”;
public function display_variable()
{
// method body
}
}

Access Modifier::

PHP access modifier used to take control of the class or class properties. At this moment we only consider public and private access modifier others will be discussed later.
public modifier—the field is accessible from all classes.
private modifier—the field is accessible only within its own class.
This access modifier used to prevent unauthorized access in PHP class. It is core concept of class encapsulation. 

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP