How to call class methods that do not exist in php?

magento, magic-methods, oop, overriding, php

Solution

You can use the magic method `__call` to solved your situation

<?php
class Called
{
    private $list = array('Id' => 1, 'Name' => 'Vivek Aasaithambi');

    public function __call($name, $arguments) {
        $field = substr($name, 3);
        echo $this->list[$field];
    }
}

$obj = new Called();
$obj->getId();
echo "<br/>\n";
$obj->getName();

?>

You can read more about `__call` in:

http://php.net/manual/en/language.oop5.overloading.php#object.call

Problem

I just want to create function just like `getFieldname()` that is in magento. For Ex: In Magento `getId()` - returns value of `ID` field `getName()` - returns value of `Name` field How can I create like that function? Kindly help me in this case.. I want to do just Like Below code, ``` Class Called{ $list=array(); function __construct() { $this->list["name"]="vivek"; $this->list["id"]="1"; } function get(){ echo $this->list[$fieldname]; } } $instance=new Called(); $instance->getId(); $instance->getName(); ```

Original source

Related problems