public、protected、private

public (public): it can be accessed from anywhere

protected (protected): can be accessed by itself and its parent class and subclass, object class can not access

private (private): class where access can only be defined, object class can not access

 

<?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; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private

Such as shown in the above code, we visit a class with the instance of an object or a class of private property protected by the members, it will throw a fatal error.

 

Access class private property, usually to write a public method, and then return to this property.

public function getPrivate()
{
 return $this->private;
}

 

Guess you like

Origin www.cnblogs.com/qq254980080/p/10993632.html