Comments (12)
Can you include the rules you set on the dice instance? By default instances are not flagged as shared.
from dice.
$dice = $dice->addRule('someclass', ['shared' => true]);
from dice.
You'll need include the namespace:
$dice = $dice->addRule('\ns\someclass', ['shared' => true]);
from dice.
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.
$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.
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.
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.
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.
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.
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.
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.
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)
- constructParams being applied in the wrong order HOT 1
- Error in documentation about json loading
- Wildcards in rules HOT 3
- Can't inherit global rules HOT 1
- Shared chain call HOT 8
- Call to undefined method ReflectionType::getName() HOT 3
- How to share a substitution HOT 1
- PHP 8 Deprecation HOT 11
- ..
- How to deal with inheritance HOT 2
- Having trouble creating PDO object HOT 3
- Performance: Opcache and Compiled container
- Support for union/intersection types? HOT 2
- Website section 4.1 missing.
- How to get a named instance from the container? HOT 1
- shareInstances are resolving but don't always propagate
- shared dependencies not working HOT 2
- PDO or Mysqli, named instances and shared instances error Undefined Array Key
- Shared dependencies by default? HOT 5
- Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, function "is_mixed" not found. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dice.