Loading

PHP As An Object Oriented Programming Part-1

 Class In PHP :-

PHP defines a class using class keyword and followed by class name and then followed by opening and closing curly braces { }. These curly braces enclose the definition of class properties and methods.

The following rule is important for defining class name:-

  1. The class name cannot be the PHP reserve keyword.
  2. The class name should start with a letter or underscore, no special symbol.
  3. class names cannot contain space.
  4. It is good practice to start the class names using capital letters.

Example of class definition:-

class Mysimpleclass{

//property

$var = ‘defined value’;

public function getvalue(){

echo $this->var

}

}

here $this is pseudo-variable. It is used for reference for calling object. that means it refers to the current object.

Instance:-  An Instance is an Object which is created for an existing class. Creating an object of existing class is called instantiating the object. Using the New keyword, we can create an object from outside of class.

As per the upper example we can create an object

$myclass = new Mysimpleclass;

Calling Member Function

$myclass->getvalue();

Note:-

var_dump()the function is used to dump information about the variable. It displays structure information like type and value of variables

instanceof PHP keyword is used to check specific objects belonging to specific classes. Like

var_dump($myclass instanceof Mysimpleclass);

Class Properties:- Class member variables are called class properties. We can declare properties using any one keyword like Private, Public or Protected. Class default property will be Public.

Public Property:- Public property can access outside of the class.

Private Property:- Private property can access only within a class.

Protected Property:- Protected property can access a class and their child’s class.

Share with:


6+

One thought on “PHP As An Object Oriented Programming Part-1

Leave a Reply

Connect with:




z35W7z4v9z8w