need help understanding some aspects of how to use variables from 1 extended php class
sorry if this seems like a really newbish question but i am in need of some help. i am trying to make a dynamic web app but having trouble with grabbing a variable from 1 extended class to another
here is an example of my current attempt so far
<?php
class a {
public $classb;
public $classc;
function load_files() 开发者_如何学C{
include_once('classb.php');
include_once('classc.php');
$this->classb = new classb();
$this->classc = new classc();
}
}
?>
and here is the class that contains the variable which i am attempting to grab
<?php
class b extends a {
public $name;
public function quessName() {
$this->name = "john";
}
}
?>
and this is the class that is trying to grab the "name" variable from c class but i cannot seem to do and php is not even bothering to return an error to at all, am i missing something really basic here... any help appreciated
<?php
class c extends a {
function retreiveName () {
echo $this->a->b->name;
or
echo parent::->b->name;
}
}
?>
I think you want c to extend b (not a). Then in c you can just call $this->name;
In class c you could try:
echo $this->$classb->$name;
(Since an object of c is also an a-object, by definition of extending, you can use $this to access a member variable declared in the a class. Also, you need to put dollar signs in front of member variables.)
精彩评论