I have found a code on php.net
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; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
Which is fine for working
But if I use if as:
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();
print_r($obj);
It gives me all the information of my class variables.
So how can I protect my class variable information if I am using it as an API code and class variable holding my database information.
You have the clear explanation for the question on this answer
.
If you still wan't to hide it , Make the variable as a static
<?php
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
static $statvar='This is a secret !'; //<---- A static variable (wont be shown)
}
$obj = new MyClass();
print_r($obj);
OUTPUT :
MyClass Object
(
[public] => Public
[protected:protected] => Protected
[private:MyClass:private] => Private
)
As you can see $statvar
is nowhere to be seen.