The Wayback Machine - https://web.archive.org/web/20210121064901/https://github.com/Ocramius/ProxyManager/issues/380
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

Partly initialization #380

Closed
AndreRudolph opened this issue Jul 21, 2017 · 5 comments
Closed

Partly initialization #380

AndreRudolph opened this issue Jul 21, 2017 · 5 comments

Comments

@AndreRudolph
Copy link

@AndreRudolph AndreRudolph commented Jul 21, 2017

Amazing and interesing project. I want to integrate your ProxyManager implementation into my project. However, I have a question about initialization of ValueHolderProxies. Is there any way to do a partly initialization depending on special use cases?

My use case:
I have a tree of objects.

    <?php
    
    class Object {
        private $parent;
        private $childs;

        public function getParent() {
            return $this->parent;
        }

        public function getChilds() {
            return $this->childs;
        }
   }`

An object within the tree can be obtained by creating a virtual proxy. Each object has parent / child relationships to other objects and some other properties which should be initialized immediately. At the moment when an object will be obtained, a proxy for the object itself and proxies for the directly parent and child objects are created. So my question is: Is there any way to create these parent / child proxies if they are explicitly needed (e.g. if getParent is called) ?

Best regards

@Ocramius
Copy link
Owner

@Ocramius Ocramius commented Jul 21, 2017

Partial loading is only possible with a ghost object, although I didn't implement it fully yet, and I'm unsure about the proper interface for now.

You probably want exactly what @lstrojny wants in #377

There is one way to achieve that, although it is very hacky and not formalised. As you can see in the initializer signature, a method and a parameter list is available. If you check for usages of __get, __set or __isset and only load the properties you want, then unset the properties that shouldn't have been initialized, that could work, although I didn't try it myself.

I will at some point have to implement partial loading, as I really want the feature for Doctrine ORM too.

@AndreRudolph
Copy link
Author

@AndreRudolph AndreRudolph commented Jul 21, 2017

Thank you for your fast reply. Apparently you have planned to implement partial loading in future, so I can wait with this performance issue because it has not a high priority. The important thing is that it works and it works like a charm.

Maybe it will reduces complexity in future if you could provide a way to use objects as initializer. Closures could be used for less complex and classes for more complex initialization logic.

Thank you very much.

@Ocramius
Copy link
Owner

@Ocramius Ocramius commented Jul 21, 2017

classes for more complex initialization logic.

That's the plan for a later version: since PHP anonymous classes are now available, I want to interface the initializer.

Ref: #334

@AndreRudolph
Copy link
Author

@AndreRudolph AndreRudolph commented Jul 21, 2017

That's a good idea. Then I will use your library without worries. 👍

@Ocramius
Copy link
Owner

@Ocramius Ocramius commented Dec 20, 2020

Note: the initializers are now interfaced correctly, although through psalm annotations.

* @psalm-param Closure(
* RealObjectType&GhostObjectInterface<RealObjectType>=,
* string=,
* array<string, mixed>=,
* ?Closure=,
* array<string, mixed>=
* ) : bool $initializer
@Ocramius Ocramius closed this Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.