ShaolinF Posted November 24, 2009 Share Posted November 24, 2009 Hi Guys, I am trying to access the $validator var from my extended class but it keeps giving me the following error message: Fatal error: Call to a member function filterStr() on a non-object Code below: require_once('FormValidator.php'); class CoursesManager { protected $validator; __construct() { $this->validator = new FormValidator(); } } // ------------------------------------------------------ class NewCourse extends CoursesManager { function useValidator() { $var = $_GET['somevar']; $var = $this->validator->filterStr($var); } } Link to comment https://forums.phpfreaks.com/topic/182795-accessing-parent-variables-in-extended-class/ Share on other sites More sharing options...
flyhoney Posted November 24, 2009 Share Posted November 24, 2009 This works for me: <?php class FormValidator { public function filterStr($foo) { return $foo; } } class CoursesManager { protected $validator; public function __construct() { $this->validator = new FormValidator(); } } class NewCourse extends CoursesManager { function useValidator() { $var = $this->validator->filterStr($_GET['somevar']); } } $newcourse = new NewCourse(); print_r($newcourse); exit; NewCourse Object ( [validator:protected] => FormValidator Object ( ) ) Link to comment https://forums.phpfreaks.com/topic/182795-accessing-parent-variables-in-extended-class/#findComment-964807 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 What version of PHP are you using? Link to comment https://forums.phpfreaks.com/topic/182795-accessing-parent-variables-in-extended-class/#findComment-964840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.