How do I get the $str variable (below) into my class/classes? The function is used to call each class dynamically rather tan have "lots" of if(class_exists) statements.
on page:
echo rh_widget('Search');
the function (on functions page):
function rh_widget($str) {
global $db, $table_prefix;
$newwidget = 'rh_'.strtolower($str);
if(class_exists($newwidget)):
$rh_wid = new $newwidget();
echo $rh_wid->rh_widget;
endif;
}
Then a Parent & child class (on classes page) such as:
class widget {
public $str;
function __construct() {
$this->before_widget .= '<ul class="rh_widget">';
$this->before_title .= '<li><h3>'.$str.'';
$this->after_title .= '</h3><ul>';
$this->after_widget .= '</ul></li></ul>';
}
}
class rh_search extends widget {
public function __construct() {
parent::__construct();
global $db, $table_prefix;
$this->rh_widget .= $this->before_widget;
$this->rh_widget .= $this->before_title.' '.$this->after_title;
$this->rh_widget .= '<li>Conte开发者_C百科nt etc. in here</li>';
$this->rh_widget .= $this->after_widget;
} }
What I cannot get to happen is to "pull" $str through from the function call through function to the class.
Any suggestion please. Thanks
I think you are trying to get access to the variable $str
from the class widget
; correct me if this is not the case.
You need to pass the variable as an argument to the constructor:
class widget {
public $str;
function __construct($str) { // add $str as an argument to the constructor
$this->before_widget .= '<ul class="rh_widget">';
$this->before_title .= '<li><h3>'.$str.'';
$this->after_title .= '</h3><ul>';
$this->after_widget .= '</ul></li></ul>';
}
}
class rh_search extends widget {
public function __construct($str) { // add $str to the constructor
parent::__construct($str); // pass $str to the parent
global $db, $table_prefix;
$this->rh_widget .= $this->before_widget;
$this->rh_widget .= $this->before_title.' '.$this->after_title;
$this->rh_widget .= '<li>Content etc. in here</li>';
$this->rh_widget .= $this->after_widget;
}
}
function rh_widget($str) {
global $db, $table_prefix;
$newwidget = 'rh_'.strtolower($str);
if(class_exists($newwidget)):
$rh_wid = new $newwidget($str); // pass $str to the constructor
echo $rh_wid->rh_widget;
endif;
}
精彩评论