Hassan Hassan - 3 months ago 9
PHP Question

why I keep getting "Fatal error: Call to undefined function getCountries()" in php oop?

I am beginner in php OOP but I have created a class called countries as the following:

countries.php

class countries {
public $countries = array("Afghanistan","Hong Kong","Moldova","Malawi");

public function getCountries() {
return $countries;
}
}


test.php

require_once('/lbs/countries.php');
$country = new countries();

print_r($country->{getCountries()});

Answer

Just change it, use $this-> if you don't call static function or variable. In your situation to get array values with $this->

public function getCountries() {
   return $this->countries;
}

and also call function from class without - {braces}, for instance, $class->function_name();

print_r($country->getCountries());
Comments