Git Product home page Git Product logo

Comments (12)

TRPB avatar TRPB commented on July 17, 2024

Can you include the rules you set on the dice instance? By default instances are not flagged as shared.

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024
$dice = $dice->addRule('someclass', ['shared' => true]);

from dice.

TRPB avatar TRPB commented on July 17, 2024

You'll need include the namespace:

$dice = $dice->addRule('\ns\someclass', ['shared' => true]);

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024

Sorry just missed to type that.

More testing, tried to remove the namespace. But added \ incase it's was the issue and it resulted in:

works
addRule('someclass', ['shared => true'])
don't
addRule('\someclass', ['shared => true'])

but the last works if I flip the calls to create

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024
$dice = $dice->addRule('\someclass', ['shared' => true]);

    class someclass {}

    class someotherclass {
        public $obj;
        public function __construct(someclass $obj){
            $this->obj = $obj;
        }
    }

    //works
    /*$a = $dice->create('\someclass');
    $b = $dice->create('\someotherclass');
    
    var_dump($a === $b->obj);*/

    //don't work, someclass is now a new instance instances
    $b = $dice->create('\someotherclass');
    $a = $dice->create('\someclass');

    var_dump($a === $b->obj);

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024

After some looking into dice seams like if I add

$name = ltrim($name, '\\');

into the create method (after line 88) everything works as expected I don't know why it isn't done there or if it's was thought be to be done else where, I can't tell.

Hope this helps.

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024

Issue seams to be with Reflection class. It will not return a class name with a leading \ infront of the class name. This causes two instance to be created.

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024

Don't know if I just should continue on this issue or start a new since it's about the same topic.

Noted that the shared instance didn't do as it should again. this time it seams like

$a = $dice->create('\Example\Class');
$b = $dice->create('\Example\Class');

echo $a === $b; //TRUE

And

class name {
    public function __construct(\Example\Class $a, \Example\Class $b){
        echo $a === $b; //TRUE
    }
}

This work as expected but produces different instances. If created from dice->create() the instance is stored with the capital letters but then injected it's with lower case. Looking at the dice $instances variable I can see two instances for the same class.

$instances: [
    ['Example\Class'],
    ['example\class']
]

I've solved this like I solved the previous problem with the case of an instance stored with a \ infront and one without. By adding on an extra function

so instead of witch solved the \ problem

public function create(string $name, array $args = [], array $share = []) {
        $name = ltrim($name, '\\');

I've added strtolower aswell.

public function create(string $name, array $args = [], array $share = []) {
        $name = ltrim(strtolower($name), '\\');

This seams to solve both the extra \ and the Case sensitive instances key issue

Hope this helps in anyway, I don't know where it should go or if I broke something else by doing this. But sharing instances works at my end now.

from dice.

TRPB avatar TRPB commented on July 17, 2024

This work as expected but produces different instances. If created from dice->create() the instance is stored with the capital letters but then injected it's with lower case. Looking at the dice $instances variable I can see two instances for the same class.

Dice is case sensitive. There was a discussion about this a few years back (though I can't see to find it now) but being case insensitive caused an unnecessary performance hit for little practical benefit.

As 99% of classes are instantiated inside the container itself, and the class name comes from reflection, which always gives the same class name casing, it wasn't really worth the performance hit of converting the case every single time.

from dice.

TRPB avatar TRPB commented on July 17, 2024

Similarly, the first issue is the same thing. I don't really want to add string manipulation to every class name if we can help it.

At the moment, a $dice->create() call from an existing shared instance is just a simple array lookup. As soon as you add string manipulation to this, Dice loses performance in benchmarks. Since most $dice->create calls should come from within the container, rather than from outside it, it's unlikely this is much of an issue in real world applications.

from dice.

TRPB avatar TRPB commented on July 17, 2024

I've fixed the first issue, as we can do this once when the instance is written to the array, but case insensitivity would require strtolower on every single call to create() which would add a performance hit to every call.

from dice.

ckrudelux avatar ckrudelux commented on July 17, 2024

Ha, well I expected it to be case sensitive, but couldn't understand why it throw a lowercase back so I thought it was doing something weird cause I always type my classes with uppercase. I did some more digging this morning and it turns out the fault was on my end. Being so sure that I typed a uppercase, but I was completely wrong. It was defined with lowercase.

The slash works with the new fix, thank you very much!

from dice.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.