0

why does it shows undefined variables. Undefined variable: classRooms. Why should I use var in class properties instead of only using $ in php.

class School {

    var $teachersCount = 10;
    var $classRooms =20;

    function teachingHours(){
         $teacherHours = array();
         $teacherHours = $this->classRooms / $this->teachersCount;
         foreach ($teacherHours as $key => $hours) {
            $teacherHours = $teacherHours -$key;
            echo $teacherHours.''.$key;
         }

    }
}

Instantiating the School class

$school = new School();
echo $school-> classRooms;
echo"<br>";
echo $school -> teachersCount;
echo"<br>";
$school-> teachingHours();
rakesh shrestha
  • 1,335
  • 19
  • 37
  • `$teacherHours = $this->classRooms / $teachersCount;` – Rotimi Mar 21 '18 at 15:37
  • 2
    *"Why should I use var in class properties instead of only using $ in php"* - you **shouldn't** unless you're writing PHP that's about 10+ years out of date. Those are properties of the object and should be declared `public`, `protected` or `private` - see [visibility](http://php.net/manual/en/language.oop5.visibility.php) – CD001 Mar 21 '18 at 15:40
  • 1
    Because you are not encapsulating. Use public, private or protected. It will work – rakesh shrestha Dec 01 '18 at 06:38

0 Answers0