Dynamicly creating class with trait binding

天大地大妈咪最大 提交于 2019-11-28 11:00:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!