Jump to content

Accessing parent variables in extended class


ShaolinF

Recommended Posts

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);
}
}

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
        (
        )

)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.