Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何输出php类设置的数组值?_Php_Arrays_Class_Oop - Fatal编程技术网

如何输出php类设置的数组值?

如何输出php类设置的数组值?,php,arrays,class,oop,Php,Arrays,Class,Oop,我希望有人能帮我解决我的问题。我还是PHP的初学者,很抱歉。 我的代码如下所示: class Component { public $title; // value if nothing gets set public function __construct($title = "Test") { $this->title = $title; } public function setTitle($value) {

我希望有人能帮我解决我的问题。我还是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;
    }
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);
        }
    }
}
因此,在类“Component”中,函数应该设置并获得一个特定的值。如果没有为a.e.标题设置任何内容,则应获取值“Test”。使用returnInfo()时,应返回标题等信息

我的另一个类(有人可以在其中添加标题等信息)如下所示:

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;
    }
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的类中,用户可以添加按钮之类的组件。然后用户可以设置像标题这样的信息。之后,信息应该保存在一个数组中,现在我想显示如下所有信息:

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;
    }
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
类中删除
abstract
关键字

更新根据您评论中的信息,我会这样做

Component.php

抽象类组件
{
私钥;
私人产权;
公共功能构造($key,$title){
$this->setKey($key);
$this->setTitle($title);
}
公共函数设置键($key)
{
$this->key=$key;
}
公共函数getKey()
{
返回$this->key;
} 
公共函数setTitle($title)
{
$this->title=$title;
}
公共函数getTitle()
{
返回$this->title;
}
公共函数
{
返回$this->getKey()。:'。$this->getTitle();
}
}
ComponentInfo.php

class ComponentInfo扩展组件
{
公共函数构造($key='Info',$title='example title')
{
父项::_构造($key,$title);
}
}
然后在代码中使用它
somefile.php

$components=[];
$components[]=新组件信息();
$components[]=新组件信息(“不同的键”,“其他信息”);
$components[]=新组件导航(“经度”、“纬度”)//创建另一个类,如ComponentInfo
[…稍后您想在列表中打印此信息,例如]
回声“
    ”; foreach($components作为$components){ echo“
  • ”.$component.
  • ”;//应自动调用_toString()方法 } 回声“
”;

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

您的意思是您想要类似于
foreach($button1Info as$label=>$info)
的东西,然后是
echo$label.:“$info?是的,但是它应该输出文本“Button Standard”,这是我用setTitle设置的。所以它仍然没有为你的答案输出任何东西。我的想法是创建多个ComponentInfo类。例如,如果我想添加一个名为“form”的组件,我想创建一个新的ComponentInfo类,在这个类中我可以设置标题或其他值。当我想创建一个像“导航”这样的新组件时,我还想构建一个ComponentInfo类,其中的标题是导航。不同组件的所有信息都应该保存到一个数组中。数组的信息应该显示在主页上,如“Title:Navigation,Description:…”(如果我添加更多像Title这样的属性),非常感谢您的快速回答。有没有办法像这样输出文本?标题:按钮[新行]说明:特定按钮[新行]。。。因此,每次有一个新的项目,一个新的行。现在它是这样的:Title:button描述:buttonIt取决于显示它的位置。我向您展示的内容将在浏览器中创建一个带有项目符号点的列表。否则,您可以删除
echo'
echo'”
并删除
  • 标记,并替换为
    echo$component.PHP\u EOL
    echo$component.