PHP ACCESS MODIFIER

Sugar babies

PHP ACCESS MODIFIER

                             •Properties and methods can have access modifiers which control where they can be accessed.

There are three access modifiers :

                  •Public - the property or method can be accessed from everywhere.
                 •Protected - the property or method can be accessed within the class and by classes derived from that class
                 •Private - the property or method can ONLY be accessed within the class.

SYNTAX FOR ACCESS MODIFIER IN PHP :

 class Animal { public void method1() {...} private void method2() {...} }

Example for access modifier in PHP :

<?php
/* Parent Class */
class ParentClass{

  /*Class Members*/
  public $A = 130;
  public $B = 70;

  public function addition(){
    echo "The sum is ", $this->A + $this->B;
  }
}

/* Child class */
class ChildClass extends ParentClass{
  public function subtract(){
    echo ", and the difference is ", $this->A - $this->B,"." ;
  }
}

$Obj = new ChildClass;
$Obj->addition() ;
$Obj->subtract() ;
?>

OUTPUT :


The sum is 200, and the difference is 60.


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send