<?php/**
* Define MyClass
*/class MyClass{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // Worksecho $obj->protected; // Fatal Errorecho $obj->private; // Fatal Error$obj->printHello(); // Shows Public, Protected and Private
/**
* Define MyClass2
*/class MyClass2 extends MyClass{
// We can redeclare the public and protected properties, but not private
public $public = 'Public2';
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj2->public; // Worksecho $obj2->protected; // Fatal Errorecho $obj2->private; // Undefined$obj2->printHello(); // Shows Public2, Protected2, Undefined
?>
public
scope to make that variable/function available from anywhere, other classes and instances of the object.private
scope when you want your variable/function to be visible in its own class only.protected
scope when you want to make your variable/function visible in all classes that extend current class including the parent class.
Public:
When you declare a method (function) or a property (variable) as
public
, those methods and properties can be accessed by:- The same class that declared it.
- The classes that inherit the above declared class.
- Any foreign elements outside this class can also access those things.
Example:
<?php
class GrandPa
{
public $name='kamran'; // A public variable
}
class Daddy extends GrandPa // Inherited class
{
function displayGrandPaName()
{
return $this->name; // The public variable will be available to the inherited class
}
}
// Inherited class Daddy wants to know Grandpas Name
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'kamran'
// Public variables can also be accessed outside of the class!
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Prints 'kamran'
Protected:
When you declare a method (function) or a property (variable) as
protected
, those methods and properties can be accessed by- The same class that declared it.
- The classes that inherit the above declared class.
Outsider members cannot access those variables. "Outsiders" in the sense that they are not object instances of the declared class itself.
Example:
<?php
class GrandPa
{
protected $name = 'kamran';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Prints 'kamran'
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
The exact error will be this:
PHP Fatal error: Cannot access protected property GrandPa::$name
Private:
When you declare a method (function) or a property (variable) as
private
, those methods and properties can be accessed by:- The same class that declared it.
Outsider members cannot access those variables. Outsiders in the sense that they are not object instances of the declared class itself and even the classes that inherit the declared class.
Example:
<?php
class GrandPa
{
private $name = 'irfan';
}
class Daddy extends GrandPa
{
function displayGrandPaName()
{
return $this->name;
}
}
$daddy = new Daddy;
echo $daddy->displayGrandPaName(); // Results in a Notice
$outsiderWantstoKnowGrandpasName = new GrandPa;
echo $outsiderWantstoKnowGrandpasName->name; // Results in a Fatal Error
The exact error messages will be:
No comments:
Post a Comment