0

for example i have 2 Classes: "Main" and "Dll_Main_Bla".

Class "Dll_Main_Bla" has static methods only! for example

public static function doIt($argument){return $argument*2;};

Inside Main I have static variable public static $dll_bla = 'Dll_Main_Bla';

In non-static method of Class Main I want To call:

$x = Dll_Main_Bla::doIt(2);

but I want to centralize initializing of my Dlls and call them like here:

// how to ??? $x = self::$dll_bla::doIt(2);

Yes, it doesn't works, but you might understand what exactly I want. :) How would I write that line to make it workable? Thanks for any proposition!

1
  • 1
    Sounds to me like your real problem is having to work with badly written code... Commented Nov 21, 2014 at 10:17

2 Answers 2

2

You can use call_user_func_array() (PHP reference link)

Small test example:

<?php

class Main {
    public static $dll_bla = 'Dll_Main_Bla';

    public function init() {
        $x = call_user_func_array(array(self::$dll_bla, 'doIt'),array(2));
        echo $x;
    }
}

class Dll_Main_Bla {
    public static function doIt($argument) {
        return $argument*2;
    }
}

$main = new Main;
$main->init();
Sign up to request clarification or add additional context in comments.

Comments

1

Consider doing this:

<?php
class Dll_Main_Bla {
    public static function toBeCalled($anyParam) {
        return 'Elo, ' . $anyParam;
    }
}

class Main {
    public static $dll_bla = 'Dll_Main_Bla';

    public function test() {
        $className = Main::$dll_bla;
        $x = $className::toBeCalled('Vincent');

        return $x;
    }
}

$main = new Main;
echo $main->test(); //should do what you expect it to do

5 Comments

Yes! It works! Thanks! But is it possible to call without temporary variable $className? Want to see shorter solution. ${self::$dll_bla}::toBeCalled('Vincent') or something like that...
for shorter way, please check @RichardBernards solution using call_user_func_array
No, thanks! Your solution looks like writes easier :)
@LINKeRxUA his solution does have a much larger footprint... Hence my solution ;)
@RichardBernards, I prefare androidavid solution, thank you for helping too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.