Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

calling a function through object

Posted on 2006-05-03
2
Medium Priority
?
211 Views
Last Modified: 2012-06-21
Hi all,

<?php
class MyClass {
    function myFunc() {
        print 'This is my function in myclass';
    }
}
?>

<?php
$myArr = array('theFunc' => 'myFunc()');
$theObj = new MyClass();
$theObj->$myArr['theFunc'];
?>

I know that the third line of this code won't work. So I need an alternate or better way of doing it.

Either the function name or the entire call should be in the array. Like:
(1) $myArr = array('theFunc' => 'myFunc()');       OR
(2) $myArr = array('theFunc' => '$theObj->myFunc()');

The actual scenario is much more complex which I can't explain it here, but a solution for this code should solve my problem.

Thank you all.
0
Comment
Question by:sangeetha
2 Comments
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 100 total points
ID: 16599261
I think this should work:
<?php
class MyClass {
    function myFunc() {
        print 'This is my function in myclass';
    }
}

$myArr = array('theFunc' => 'myFunc');
$theObj = new MyClass();
$theObj->$myArr['theFunc']();
?>
0
 
LVL 6

Accepted Solution

by:
ixti earned 240 total points
ID: 16602763
Hope this is clear and will help you:

<?php
class MyClass
{
    function MyClass()
    {
    }
   
    function MyFirstFunc()
    {
        echo "Sample\n";
        return true;
    }
   
    function MySecondFunc()
    {
        $a = array("pineapple", 1 => "oranges");
        return $a;
    }
   
    function MyThirdFunc($firstParam)
    {
        return $firstParam * 10;
    }
   
    function __function($functionName, $params = array())
    {
        $functionName = str_replace("()", "", $functionName);
        return call_user_func_array(array($this, $functionName), $params);
    }
}

$funcs  = array('first' => 'MyFirstFunc', 'second' => 'MySecondFunc', 'third' => 'MyThirdFunc');

$obj    = new MyClass();

var_dump($obj->__function($funcs['first']));
// Result:
// Sample
// bool(true)

var_dump($obj->__function($funcs['second']));
// Result:
// array(2) {
//   [0]=>
//   string(9) "pineapple"
//   [1]=>
//   string(7) "oranges"
// }

var_dump($obj->__function($funcs['third'], array(100)));
// Result:
// int(1000)
?>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question