Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot Call Static Methods of External Classes [$10] #916

Closed
AKermodeBear opened this issue Apr 24, 2015 · 8 comments
Closed

Cannot Call Static Methods of External Classes [$10] #916

AKermodeBear opened this issue Apr 24, 2015 · 8 comments

Comments

@AKermodeBear
Copy link

AKermodeBear commented Apr 24, 2015

Hello,

Zephir allows us to create objects that are not currently defined in the project like so:

let someObject = new \Class_From_Elsewhere();

However we cannot call static methods as might be expected:

let someResult = \Class_From_Elsewhere::someMethod("foo");

I'm currently using this as a workaround:

let someResult = call_user_func_array(
   ["\Class_From_Elsewhere", "someMethod"], ["foo"]);

Thank you, Zephir people!

Using Zephir at commit 416c989 (Wed Apr 22).

@steffengy
Copy link
Contributor

Alternatively: \Class_From_Elsewhere::{"someMethod"}("foo"); should work

@phalcon phalcon changed the title Cannot Call Static Methods of External Classes Cannot Call Static Methods of External Classes [$10] Apr 24, 2015
@phalcon phalcon added the bounty label Apr 24, 2015
@ovr
Copy link
Contributor

ovr commented Apr 25, 2015

I think warning was caused by \Class_From_Elsewhere does not exist
You need to use prototype with autoload for it class

Can you show full compiler exception?

@ovr
Copy link
Contributor

ovr commented Apr 27, 2015

ping @Stormwing

@AKermodeBear
Copy link
Author

Howdy,

The compiler will produce an error like this:

Zephir\CompilerException: Class name: MyClass does not exist in /home/bear/repos/zephir-sandbox/test/test/Test.zep on line 6

          return \MyClass::method();
        ---------------------------^

@valVk
Copy link

valVk commented May 6, 2015

ping @Stormwing

Maybe this one will help you.

namespace Utils;

class SomeClass {
     public function getClassName()
     {
        var className = "NotExists";
        return {className}::method();
     }
}
#!/usr/bin/env php
<?php

class NotExists
{
    public static function method()
    {
        return "Hello from big Hangout";
    }
}


$a = new \Utils\SomeClass(); 
var_dump($a->getClassName());

@sergeyklay sergeyklay removed the bounty label Oct 11, 2017
@fabiante
Copy link

fabiante commented Mar 4, 2020

I am having the same problem. Neither of the workarounds work for me.

\Class_From_Elsewhere::{"someMethod"}("foo"); throws a compiler error and the call_user_func_array workaround just throws a class ... not found error during runtime.

@sergeyklay
Copy link
Contributor

@fabiante To achieve better performance consider use create_instance_params and avoid call_user_func_array as long as you can.

@fabiante
Copy link

fabiante commented Mar 5, 2020

We figured out, that this works, as long as double backslashes are used:

let someResult = call_user_func_array(
   ["\\MyNamespace\\Subnamespace\\MyNormalClass", "someMethod"], ["foo"]);

@sergeyklay Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants