问题
I want to make use of traits in my project, and for multiple inheriance I want to use traits.
So I created some traits to use eg: tItem_Epic, tItem_Weapon, Item_Driver
When I create new class for Sword, I thought I could use eval to create class:
<?php
function new_item_class($type)
{
eval('class Item_'.ucfirst($type).' extends Item_Driver { use tItem_Epic, tItem_Weapon; }');
}
?>
This is an example. There are some more parameters that change the course of eval (like: item quality, etc.).
Does this slow down the progress? Or should I create a file for every item type and call them when needed? which one will be faster?
回答1:
You could generate the files on disk which could be a benefit with IDEs as they can parse those files as well and it's less magic.
For PHP there is not much difference between eval
(your code-exmaple) and include
(example with files on disk). So do what pleases you I'd say.
I personally prefer files because it's more direct than those magic classes. And I would not look for "performance" reasons to decide that. The kind of performance you talk about is short-sighted as it remains undefined about which kind of performance you talk about and especially because you did not yet run into a bottleneck.
来源:https://stackoverflow.com/questions/10461965/dynamicly-creating-class-with-trait-binding