Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
917 views
in Technique[技术] by (71.8m points)

php - Dynamicly creating class with trait binding

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...