PHP on access control and operator precedence

  • 2020-07-21 07:21:24
  • OfStack


class Foo 
{ 
    private $name = 'hdj'; 
    public function getName(){ 
        return $this->name; 
    } 
} 

class Bar extends Foo 
{ 
    public $name = 'deeka'; 
} 

$bar = new Bar; 
var_dump($bar->name); 
var_dump($bar->getName());

Access control

Access control to a property or method is achieved by adding the keywords public, protected, or private in front of it. Class members defined by public can be accessed anywhere; A class member defined by protected can be accessed by a subclass and a parent of the class in which it belongs (and, of course, by the class in which it belongs); A class member defined by private, on the other hand, can only be accessed by its class.


<?php
$a = 3;
$b = 6;
if($a = 5 || $b = 7){
    echo $b.'<br />';
    $a++;  
    $b++;
}
var_dump($a, $b);
echo '<br /> $a = (5 || $b = 7)';
echo '<hr />';
$a = 3;
$b = 6;
$c = 1;
if($a = 5 || $b = 7 && $c = 10){
    $a++;  
    $b++;
}
var_dump($a, $b,$c);
echo '<br /> &&  than  ||  high ';
echo '<hr />';
$a = 3;
$b = 6;
$c = 1;
if($a = 0 || $b = 7 && $c = 10){
    $a++;  
    $b++;
}
var_dump($a, $b,$c);
echo '<br /> ';
echo '<hr />';
class Foo {
    private $name = 'hdj';
    public function getName() {
        return $this->name;
    }
}

class Bar extends Foo {
    public $name = 'deeka';
}

$bar = new Bar;
var_dump($bar->name);
var_dump($bar->getName());


Related articles: