【问题标题】:How to output value of array which is set by php class?如何输出由 php 类设置的数组的值?
【发布时间】:2019-03-18 07:56:44
【问题描述】:

我希望有人可以帮助我解决我的问题。我仍然是 PHP 的初学者,非常抱歉。 我的代码如下所示:

class Component
{
    public $title;

    // value if nothing gets set
    public function __construct($title = "Test") {
        $this->title = $title;
    }

    public function setTitle($value)
    {
        $this->title = $value;
    }

    public function getTitle()
    {
        return "Title: ".$this->title;
    } 

    public function returnInfo()
    {
        $info = array(
        'Titel'         => $this->title,            
        );      

        return $info;
    }

因此,在“组件”类中,函数应该设置并获取特定值。如果没有为 a.e 设置任何内容。标题它应该得到值“测试”。使用 returnInfo() 应该返回标题等信息。

我的其他课程(有人可以添加标题等信息)如下所示:

abstract class ComponentInfo extends Component
{
    protected function getComponentInfo ()
    {
        $button1 = new Component;
        $button1->setTitle("Button-Standard");

        // should return all infos for button1
        $button1Info = $button1->returnInfo();

        foreach ($button1Info as $info)
        {
            echo ($info);
        }
    }
}

所以它应该像这样工作:在另一个名为 ComponentInfo 的类中,用户可以添加一个像按钮一样的组件。然后用户可以设置标题等信息。之后信息应该保存在一个数组中,现在我想显示所有信息,如下所示:

标题:按钮标准

它是如何工作的?我的代码中的错误在哪里?

获得一个工作代码会很有帮助,用户可以在其中创建他想要的任意数量的 ComponentInfo 类,并且他可以添加不同的组件以及可以保存到数组中的信息。

最后它应该在主页上作为文本输出。

【问题讨论】:

  • 你的意思是你想要foreach ($button1Info as $label => $info)然后echo $label.":".$info;这样的东西吗?
  • 是的,但它应该输出我用 setTitle 设置的文本“Button-Standard”。所以它仍然没有输出任何东西

标签: php arrays class oop


【解决方案1】:

您不能实例化抽象类。您需要从 ComponentInfo 类中删除 abstract 关键字。

更新鉴于您评论中的信息,我会这样做

Component.php

abstract class Component
{
    private $key;
    private $title;

    public function __construct($key, $title) {
        $this->setKey($key);
        $this->setTitle($title);
    }

    public function setKey($key)
    {
        $this->key = $key;
    }

    public function getKey()
    {
        return $this->key;
    } 

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function __toString()
    {
        return $this->getKey().': '.$this->getTitle();
    }

}

ComponentInfo.php

class ComponentInfo extends Component
{
    public function __construct($key='Info', $title='example title')
    {
        parent::__construct($key, $title);
    }
}

然后在你的代码中使用它 somefile.php

$components = [];
$components []= new ComponentInfo();
$components []= new ComponentInfo('Different Key', 'Other info');
$components []= new ComponentNavigation('longitude', 'latidude'); //create another class like ComponentInfo

[... later you want to print this info in a list for example]

echo '<ul>';
foreach($components as $components) {
    echo '<li>'.$component.'</li>'; //the __toString() method should get called automatically
}
echo '</ul>'; 

这应该可行,但是,除了不同的标题和键之外没有其他特殊性的不同组件是没有意义的。相反,您可以简单地拥有不同的 Components,并使用不同的键和标题。

【讨论】:

  • 感谢您的回答。我的想法是创建多个 ComponentInfo 类。例如,如果我想添加一个名为“form”的组件,我想创建一个新的 ComponentInfo 类,我可以在其中设置标题或其他值。当我想创建一个像“导航”这样的新组件时,我还想构建一个 ComponentInfo 类,其中 a.e.标题是导航。并且不同组件的所有信息都应该保存到一个数组中。数组的信息应该显示在主页上,如“标题:导航,描述:...”(如果我添加更多属性,如标题)
  • 非常感谢您的快速答复。有没有办法像这样输出文本?标题: Button [new line] 描述: 一个特定的 Button [new line] ... 这样每次新项目都有一个新行。现在是这样的: Title: button Description: a button
  • 这取决于你在哪里显示它。我向您展示的内容将在浏览器中创建一个带有项目符号的列表。否则,您可以删除echo '&lt;ul&gt;'echo '&lt;/ul&gt;' 并删除&lt;li&gt; 标签,并替换为echo $component.PHP_EOLecho $component.'&lt;br/&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 2014-07-19
相关资源
最近更新 更多