5

is it possible to do something like this in php? I want to have a namespace in a member variable and just always be able to call every static method of that class like I'm doing below.

Of course my code doesn't work, but I'm just wondering if that is possible at all and that I'm close to a solution, or if that's completely out of the question and must always use the syntax:

\Stripe\Stripe::setApiKey(..);

Similar question for clarifications

NOTE: I cannot modify the Stripe class, it's important it stays untouched for when future devs must update the Stripe API

Simplified code:

class StripeLib
{
    var $stripe;

    public function __construct()
    {
        // Put the namespace in a member variable
        $this->stripe = '\\'.Stripe.'\\'.Stripe;
    }
}

$s = new StripeLib();

// Call the static setApiKey method of the Stripe class in the Stripe namespace
$s->stripe::setApiKey(STRIPE_PRIVATE_KEY);

2 Answers 2

1

Yes something like this is possible. There is is static class method which can be called which returns the namespace path of the class.

<?php
namespace Stripe;

Class Stripe {

    public static function setApiKey($key){
        return $key;    
    }

}

class StripeLib
{
    public $stripe;

    public function __construct()
    {
        // Put the namespace in a member variable
        $this->stripe = '\\'.Stripe::class;
    }
}

$s = (new StripeLib())->stripe;

// Call the static setApiKey method of the Stripe class in the Stripe namespace
echo $s::setApiKey("testKey"); //Returns testkey
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your prompt answer Daan. Is there a way to make it work without having to use $stripeLib, only use $s? I fear this will immensely confuse other devs
@NaturalBornCamper Yes of course, I've edited my answer.
So basically this is just like the other question I linked, it absolutely has to be in a regular variable? It cannot be in the form of $this->member::setApiKey("testKey")?
No unfortunately not.
Ok thanks, I guess it's not really what I was hoping for, for you did answer my question by telling me it was impossible to do it the way I wanted it to, so thanks! :)
0

I just tested it, yes you can do that in php.

But I think you violate Dependency Injection principle here. The right way to do that is:

class StripeLib
{
    var $stripe;

    // make sure Stripe implements SomeInterface
    public function __construct(SomeInterface $stripe)
    {
        // Stripe/Stripe instance
        $this->stripe = $stripe;
    }
}

1 Comment

Interesting answer Keloo, thanks for that. I can't edit the Stripe class however, it's an official API and I would like to keep it intact for when future developers update the library after I'm gone, without wondering why it's broken ;) Should have mentioned that, I'll do it now

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.