C++ Templates but for PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mherbold
Forum Newbie
Posts: 1
Joined: Tue Nov 02, 2010 5:25 pm

C++ Templates but for PHP?

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: C++ Templates but for PHP?

Post 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++.
Post Reply