在下面的代码中我有两个类。我想从 IT_HTML_Content 类获得回显,然后该回显将以参数的形式传递给 IT_HTML_Panel 并移交给其位置,以使用第一类的现有回显执行第二类的回显。这可能吗?这是一个好方法吗?也许我应该使用一个类来扩展另一个类。我想要一些建议。
<?php
class IT_HTML_Panel {
protected $name;
protected $surname;
protected $index;
public function __construct($args) {
$this->name = $args['name'];
$this->surname = $args['surname'];
$this->index = $args['index'];
}
public function add_node() {
echo "Name: {$this->name}, Surname: {$this->surname}, Index: {$this->index}";
}
}
class IT_HTML_Content {
protected $indexNumber;
public function __construct($args_con) {
$this->indexNumber = $args_con['indexNumber'];
}
public function add_node() {
echo "Index Number: {$this->indexNumber}";
}
}
$args = array(
'name' => 'Mickey',
'surname' => 'Mouse',
'index' => $args_con = array(
'indexNumber' => '12345'
)
);
// Create instances of IT_HTML_Panel and IT_HTML_Content classes with the provided arguments
$it_html_panel = new IT_HTML_Panel($args);
$it_html_content = new IT_HTML_Content($args_con);
// Call the add_node method for both classes to display the values
$it_html_panel->add_node();
$it_html_content->add_node();
?>
我建议您在另一个类中创建第一个类的实例(像我一样在构造函数或方法中)并使用第二个类返回一个字符串,您将在
echo
中使用该字符串。
我想从 IT_HTML_Content 类中得到回显,然后它将是 以参数的形式传递
echo 不能作为参数传递,它只是“打印”到页面
<?php
class IT_HTML_Panel {
private string $name; // changed to private as this class is not being extended
private string $surname;
private string $index;
public function __construct(array $args) {
$this->name = $args['name'];
$this->surname = $args['surname'];
$this->index = $args['index'];
}
public function add_node(): void {
$itHtmlContent = new IT_HTML_Content($this->index);
$index = $itHtmlContent->get_index();
echo "Name: {$this->name}, Surname: {$this->surname}, Index: {$index}";
}
}
class IT_HTML_Content {
private string $indexNumber;
public function __construct(array $args_con) {
$this->indexNumber = $args_con['indexNumber'];
}
public function get_index(): string {
return "Index Number: {$this->indexNumber}";
}
}
$args = [
'name' => 'Mickey',
'surname' => 'Mouse',
'index' => [
'indexNumber' => '12345'
]
]; // changed array() to [] - https://stackoverflow.com/questions/26651996/php-array-vs-in-method-and-variable-declaration
// Create instances of IT_HTML_Panel class with the provided arguments
$it_html_panel = new IT_HTML_Panel($args);
// Call the add_node method for both classes to display the values
$it_html_panel->add_node();
?>
或者如果你不想返回字符串,你可以这样做:
<?php
class IT_HTML_Panel {
private string $name; // changed to private as this class is not being extended
private string $surname;
private array $index;
public function __construct(array $args) {
$this->name = $args['name'];
$this->surname = $args['surname'];
$this->index = $args['index'];
}
public function add_node(): void {
$itHtmlContent = new IT_HTML_Content($this->index);
echo "Name: {$this->name}, Surname: {$this->surname}, Index: ";
$itHtmlContent->add_node()
}
}
class IT_HTML_Content {
private string $indexNumber;
public function __construct(array $args_con) {
$this->indexNumber = $args_con['indexNumber'];
}
public function add_node(): void {
echo "Index Number: {$this->indexNumber}";
}
}
$args = [
'name' => 'Mickey',
'surname' => 'Mouse',
'index' => [
'indexNumber' => '12345'
]
]; // changed array() to [] - https://stackoverflow.com/questions/26651996/php-array-vs-in-method-and-variable-declaration
// Create instances of IT_HTML_Panel class with the provided arguments
$it_html_panel = new IT_HTML_Panel($args);
// Call the add_node method for both classes to display the values
$it_html_panel->add_node();
?>
作为旁注,我要补充一点,您似乎正在使用非常古老的 PHP 编写代码(<5.4), so I "updated" the code a little bit by adding types and left comments where I changed something. Applying features of newer PHP versions will help you write more readable code and less bugs.