PHP - 如何从“外部”设置多维数组属性的值班级?

发布于 2024-09-30 07:05:57 字数 996 浏览 1 评论 0原文

我正试图解决一些我似乎无法找到解决方案的问题。在AI类中,有一个属性是多维数组。在类 A 中实例化的类 B 中,我想为类 A 中的属性设置一个索引值。我遇到了困难,我不知道如何迭代该属性来设置索引 I 的值需要。这是我的意思的测试示例:

<?php

class SomeClass
{
    protected $class;
    protected $book;


    public function __construct()
    {
        $this->book["genre"]["title"] = "The Haunting";
    }


    public function SomeTest()
    {
        $this->class = new AnotherClass();
        $this->class->AnotherTest();
    }
}

class AnotherClass
{
    protected function setBook()
    {
        $indexes = func_get_args();
        $value   = array_pop($indexes);

        /**
         * So now I have the indexes which lead to the array
         * depth I'd like to set a value to, but where do I
         * go from here?
         *
         */
    }

    public function AnotherTest()
    {
        $this->setBook("something", "else", "1984");
    }
}



$someclass = new SomeClass();
$someclass->SomeTest();

?>

我对如何做我想做的事感到非常迷失。

I'm trying to wrap my head around something that I can't seem to figure out a solution for. In class A I have a property which is a multidimensional array. In class B which is instantiated within class A, I'd like to set an index value for the property from class A. I run into a wall where I have no idea how to iterate through the property to set the value for the index I'd need to. Here's a test example of what I mean:

<?php

class SomeClass
{
    protected $class;
    protected $book;


    public function __construct()
    {
        $this->book["genre"]["title"] = "The Haunting";
    }


    public function SomeTest()
    {
        $this->class = new AnotherClass();
        $this->class->AnotherTest();
    }
}

class AnotherClass
{
    protected function setBook()
    {
        $indexes = func_get_args();
        $value   = array_pop($indexes);

        /**
         * So now I have the indexes which lead to the array
         * depth I'd like to set a value to, but where do I
         * go from here?
         *
         */
    }

    public function AnotherTest()
    {
        $this->setBook("something", "else", "1984");
    }
}



$someclass = new SomeClass();
$someclass->SomeTest();

?>

I'm so lost on how to do what I'm thinking of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-10-07 07:05:57

启动类 另外一个带参数,传递启动类!

class SomeClass{
    public function SomeTest()    {
        $this->class = new AnotherClass($this);
        $this->class->AnotherTest();
    }
}

我在这些情况下使用 Creator!

class AnotherClass{
    function __contruct($creator){
        $this->creator=$creator
    }

    function AnotherTest(){
        $this->creator->setBook("something", "else", "1984");
    }

initiate class Another with a parameter, pass initiator class!

class SomeClass{
    public function SomeTest()    {
        $this->class = new AnotherClass($this);
        $this->class->AnotherTest();
    }
}

I use creator for these situations!

class AnotherClass{
    function __contruct($creator){
        $this->creator=$creator
    }

    function AnotherTest(){
        $this->creator->setBook("something", "else", "1984");
    }
瀞厅☆埖开 2024-10-07 07:05:57

在这种情况下为什么不使用继承呢?你的财产受到保护,我认为在课堂之外不应该访问它。

你尝试过继承吗?

谢谢。

In such a situation why you are not using inheritance? Your property is protected and I think it should not be accessible outside the class.

Hay you tried with inheritance?.

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文