Question: I want to create an object based on the value of a variable. I hope the following code can explain better what I want:

```
$('.tariffCostState').each(function(){
        var tariffCostObj = $(this).data('tariff-code-name');

/*
    How do I set the value of tariffCostObj as an object ?

[EDIT]
        Imagine that var tariffCostObj='abc';
        I want that abc becomes an object: abc={}
*/
        var testObj={};

        var tariffCostsInput=$(this).find('input.m2m-tariff-costs-input');

        for(var j= 0, inputLen = tariffCostsInput.length; j<inputLen; j++){
            var tariffCostsInputId = $(this).find('input.m2m-tariff-costs-input').eq(j).attr('id');
            var tariffCostsInputValue= $(this).find('input.m2m-tariff-costs-input').eq(j).val();

            testObj[tariffCostsInputId]=tariffCostsInputValue;

            //The goal is to have:
            abc[tariffCostsInputId]=tariffCostsInputValue;

        }
        console.log(testObj);

    });

```

Instead of an object called `testObj` I want the value of `tariffCostObj` (abc, ... ) The testeObj is just an example

Thanks

Answer: 
Here's an idea of a possible solution you could try. Let the `Cow` and `Human` classes extend the `Entity` class. However, the `Entity` class would use a [factory](https://en.wikipedia.org/wiki/Factory_method_pattern) to instantiate the objects based on if the value was safe. Let's look at this in more detail:

```
/*
 * Class Entity should not be able to be instantiated.
 * It should contain a factory to instantiate the
 * appropriate entity and an abstract function declaring 
 * the method that each entity will need to implement.
 */
abstract class Entity {

    public static function factory($type) {
        return (is_subclass_of($type, "Entity")) ? new $type() : FALSE;
    }

    abstract public function sayHi();

}

/*
 * Human class extends Entity and implements the
 * abstract method from Entity.
 */
class Human extends Entity {

    public function sayHi() {
        echo "Hello World!";
    }

}

/*
 * Cow class extends Entity and implements the
 * abstract method from Entity.
 */
class Cow extends Entity {

    public function sayHi() {
        echo "Moo!";
    }

}

```

Now to use this method, call the factory method and if all works well, it'll instantiate the proper class which will extend `Entity`.

```
$person = Entity::factory("Human");
$person->sayHi();

$cow = Entity::factory("Cow");
$cow->sayHi();

```

Using, `is_subclass_of()` will keep you safe because if the passed in value is not a class that extends `Entity`, you'll be returned a value of `FALSE`.

If you'd like to see the above code in action, copy the above php code and test it out on [phpfiddle.org](http://phpfiddle.org/).
One thing you can do is create `Human` and `Cow` as subclasses of `Entity`. When you do `new Entity("Human")`, you can store a newly created `Human` object *inside* the `Entity` instance.

Then you can use `__call` to redirect method calls to the "child element".

```
class Entity{
    private $child;

    public function __construct($type){
        $this->child = new $type;
    }

    public function __call($func, $params=array()){
        $method = method_exists($this, $func)
            ? [$this, $func] : [$this->child, $func];
        return call_user_func_array($method, $params);
    }
}

class Human extends Entity{
    public function __construct(){}

    public function sayhi(){
        echo "Hello world!";
    }
}

class Cow extends Entity{
    public function __construct(){}

    public function sayhi(){    
        echo "Moo!";
    }
}

$person = new Entity("Human");
$person->sayhi();

$cow = new Entity("Cow");
$cow->sayhi();

```

The only downside is that `$person` and `$cow` are both `Entity` objects.