Page 1 of 1

C++ Templates but for PHP?

Posted: Tue Nov 02, 2010 5:28 pm
by mherbold
Anything like C++ templates but for PHP out there???

I have a LOT of code in my project that could be eliminated through code reuse using something like C++ templates. Is there such a thing for PHP out there?

(Note that I am not talking about presentation templates like Smarty)

Maybe a plugin into Zend Studio that would generate code using these templates???

Thanks!
Marvin

Re: C++ Templates but for PHP?

Posted: Tue Nov 02, 2010 11:39 pm
by Weirdan
mherbold wrote: I have a LOT of code in my project that could be eliminated through code reuse using something like C++ templates. Is there such a thing for PHP out there?
Well, that depends on the issue at hand (could you elaborate, btw?). When I was programming in C++ - and that was a looong time ago, so my memory could be a little dim - the main use case for templates was implementing generics in a type-safe fashion. Since PHP is dynamically typed language it's not an issue here - you don't need SplMaxHeap<Transaction> because there's no compile-time type checks and you could simply use SplMaxHeap with whatever type you need.

Of course, there are some common use cases for code generation in PHP - generating local stubs from wsdl definitions, or generating active record classes from database structure, or CRUD scaffolding based on aforementioned active record class structures - however none of them require extending the base language in a way templates are extending C++.