See the comments under the question for more information -- I'll try to summarize, as it might be useful to other :-)
First of all, I suppose the goal is not having users create almost empty files by themselves -- I remember another question, I think it was from you, where that popped up.
If so, why not just create those almost empty files dynamically ?
I mean, some idea like that :
if (condition matches) {
include file containing class definition
} else {
create another file containg the alternate class definition
include that other file
}
@ Gordon işler biraz daha kolay yapar, bir açıklama, bir olasılık, dediği gibi, kod-nesil bu göreve yardımcı olmak için, Zend_CodeGenerator a> kullanmak olacaktır.
As pointed out : the users will not likely have write-access on the library directory, but nothing prevents you from creating those files into another "cache" directory -- afterall, it's likely that your application has write-access to at least one directory :
- Önbellek için ^ ^
- Ya da, en azından, günlükleri için
A solution would be to use eval:
- sınıf 'tanımını içeren bir dize oluşturmak
eval o UATE
Ama gerçekten en az iki nedenden dolayı, bunu yapmazdım:
- Hatta neredeyse boş bir dosya / dizesi - hata ayıklama muhtemelen zor olacak
- Bir dosyayı kullanarak opcode önbelleğe (APC, or similar tools) için bir olasılık var demektir
- Ve üçüncü olarak, bu konuda düşünme: Bir dosyayı sahip IDE bunu ayrıştırmak anlamına gelir - kod tamamlanması ile yardımcı olabilir, tip-ima ...
Seeing your update, I tried conditional declaration :
if (false) {
class A {
public $a = 10;
}
} else {
class A {
public $a = 20;
}
}
$a = new A();
var_dump($a);
Gerçek ve tersi false durumu değiştirme, ben iki alternatif çıktı almak:
object(A)[1]
public 'a' => int 20
object(A)[1]
public 'a' => int 10
So... well, seems to be working!
(I'm surprised, I thought it was only possible for functions!)
But, still, I don't really like that idea... Not sure how it behaves in IDEs -- and I like having one class per file...
(Yeah, personnal opinion)