tags:

views:

36

answers:

1

How do I echo the value from the object? In the following example I expect 1105 service number to be printed after "case".

    $vbk->set('service_no','1105');
    echo "case".$vbk->service_no;

I need to use object for this purpose.

+2  A: 

Implement a __get function which will handle that logic. POC:

<?php
class service
{
    private $properties = array();

    public function set($key, $value)
    {
     $this->properties[$key] = $value;
    }

    public function __get($key)
    {
     if(isset($this->properties[$key]))
      return $this->properties[$key];

     return null;
    }
}

$service = new service;
$service->set('service_no','1105');

// case1105
echo "case".$service->service_no;
alexn
The default value would rather be `null` than an empty string.
Gumbo
Yep, you are right.
alexn