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

How to handle resources #346

Open
mario-deluna opened this issue Aug 9, 2017 · 0 comments
Open

How to handle resources #346

mario-deluna opened this issue Aug 9, 2017 · 0 comments

Comments

@mario-deluna
Copy link

Hello, first off thank all of you for all the work that goes into this library.

My question is how do I handle or create a resource?

My current workaround is wrapping the resource / pointer in a Php::Base class.

class ResourceWrapper : public Php::Base
{
private:
    MyResource *_resource = nullptr;

public:
	ResourceWrapper() : _resource(MyResource()) {}
        ~ ResourceWrapper() {}
    
        inline MyResource *getResource() { return _resource; }
};

and then using it like this:

void ClassThatDoesSomethingWithResource::setResource(Php::Parameters &parameters)
{
    Php::Value object = parameters[0];
    
    if (!object.instanceOf("ResourceWrapper")) throw Php::Exception("First argument must be an instance of ResourceWrapper.");
    
    ResourceWrapper *wrapper = (ResourceWrapper *)object.implementation();
    
    MyResource *resource = wrapper->getResource();
}

Is there a more elegant approach to hold the pointer inside PHP?

Many thanks Mario.

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

No branches or pull requests

1 participant