简体   繁体   中英

PHP combine $this variable

How to combine two variables to obtain / create new variable?

public $show_diary = 'my';

private my_diary(){
  return 1;
}

public view_diary(){
 return ${"this->"}.$this->show_diary.{"_diary()"}; // 1
 return $this->.{"$this->show_diary"}._diary() // 2
}

both return nothing.

Your class should be like following:

class Test
{
    public $show_diary;

    function __construct()
    {
        $this->show_diary = "my";
    }
    private function my_diary(){
      return 707;
    }

    public function view_diary(){
        echo $this->{$this->show_diary."_diary"}(); // 707
    }
}

It almost looks from your question like you are asking about how to turn simple variables into objects and then how to have one object contain another one. I could be way off, but I hope not:

So, first off, what is the differnce between an object and a simple variable? An object is really a collection of (generally) at least one property, which is sort of like a variable within it, and very often functions which do things to the properties of the object. Basically an object is like a complex variable.

In PHP, we need to first declare the strucutre of the object, this is done via a class statement, where we basicaly put the skeleton of what the object will be into place. This is done by the class statement. However, at this point, it hasn't actually been created, it is just like a plan for it when it is created later.

The creation is done via a command like:

 $someVariable= new diary();

This executes so create a new variable, and lays it out with the structure, properties and functions defined in the class statement.

From then on, you can access various properties or call functions within it.

class show_diary
{
    public $owner;

    public function __construct()
    {
        $this->owner='My';
    }
}

class view_diary
{
    public $owner;
    public $foo;

    public function __construct()
    {
        $this->foo='bar';
        $this->owner=new show_diary();
    }
}

$diary= new view_diary();
print_r($diary);

The code gives us two classes. One of the classes has an instance of the other class within it.

I have used constructors, which are a special type of function that is executed each time we create a new instance of a class - basically each time we declare a variable of that type, the __construct function is called.

When the $diary= new view_diary(); code is called, it creates an instance of the view_diary class, and in doing so, the first thing it does is assigns it's own foo property to have the value 'bar' in it. Then, it sets it's owner property to be an instance of show_diary which in turn then kicks off the __construct function within the new instance. That in turn assigns the owner property of the child item to have the value 'My'.

If you want to access single properties of the object, you can do so by the following syntax:

echo $diary->foo;

To access a property of an object inside the object, you simply add more arrows:

echo $diary->owner->owner;

Like this?

$diary = $this->show_diary . '_diary';
return $this->$diary();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM