Reputation: 1757
I dont know if its possible in PHP. I tried this code but I got error.
class UserAccount extends classDatabase
{
$validate = new classValidate;
function login($user, $pass)
{
//access a method from classValidate
$validate->checkInput($user);
}
}
I want to validate user input by using a method from classValidate
. How to do that? I can't extend classValidate
because I already extend classDatabase
.
Please tell me a way how to do this.
Upvotes: 0
Views: 60
Reputation: 6824
class UserAccount extends classDatabase
{
$validate = null;
function __construct(){
$this->validate = new classValidate;
}
function login($user, $pass)
{
//access a method from classValidate
$this->validate->checkInput($user);
}
}
Or with scope for added to make it not throw warnings on PHP5
class UserAccount extends classDatabase
{
private $validate = NULL;
function __construct(){
$this->validate = new classValidate();
}
public function login($user, $pass)
{
//access a method from classValidate
$this->validate->checkInput($user);
}
}
Upvotes: 0
Reputation: 20753
Your example code has a syntax error:
class UserAccount extends classDatabase
{
$validate = new classValidate; // <-- in php you can't do this
First you need to specify an access modifier like public
, protected
, private
or at least var
. Second, php can't "run" code in the declaration, the = new ClassValidate
will not work here, only constants or literals like 42
or "some string"
or results of a static method call (see docs)
You can move the instantiation to the constructor like this:
public function __construct() {
$this->validate = new ClassValidate;
}
Note the explicit $this->
, in php you will always have to type this out.
Finally you need to use the instance in login()
properly:
function login($user, $pass) {
$this->validate->checkInput($user);
}
Upvotes: 1