-1

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.

tonoslfx
  • 3,422
  • 15
  • 65
  • 107
  • 2
    In theory this should work: `return $this -> { $this -> show_diary . '_diary'} ();` Also see this: http://www.php.net/manual/en/language.oop5.overloading.php#object.call – biziclop Sep 09 '13 at 10:47
  • 1
    This is horrible to maintain and read, you should consider cleaner solutions. Anyway try this: `return $this->{$this->show_diary.'_diary'}()` – dan-lee Sep 09 '13 at 10:47
  • not sure what exactly you trying to do. Define your desire output with input. – Sumit Gupta Sep 09 '13 at 10:47
  • This is so wrong. Don't try to build function calls dynamically. – Royal Bg Sep 09 '13 at 10:48
  • I think he is trying to implement polymorphism in a wrong way. – Arun Killu Sep 09 '13 at 10:53
  • you're posting syntax errors that should make your eyes bleed: `public view_diary()` should be `public function view_diary()`... `function` is _not optional!_ – Elias Van Ootegem Sep 09 '13 at 11:17

3 Answers3

2

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
    }
}
Bora
  • 10,529
  • 5
  • 43
  • 73
1

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;
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • @tonoslfx I made a few additions pretty much the time this comment came through explaining in a bunch more detail what is going on if you care to read. – Fluffeh Sep 09 '13 at 11:08
0

Like this?

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