views:

211

answers:

2

I have developed many web applications in ASP.NET in which I have make use of UserControls to manage header footer and any other common part of a web page. Now I am developing an app in PHP, is there anything which I can use as an alternative of UserControls??

How I can implement the similar concept in PHP?

Thanks?

+1  A: 

The UserControl concept does not explicitly exist in PHP, though you can encapsulate the the functionality that you want in a file and use the include() function to place it into your pages where you wish.

<html>
 <head>
   <title></title>
 </head>
 <body>
   <?php include('header.php'); ?>
   <?php include('pageBody.php'); ?>
   <?php include('footer.php'); ?>
 </body>
</html>

Note that Smarty provides for much better separation of processing and rendering, and will make this type of thing super smooth.

Matthew Vines
I know the include very well, but using include I can't pass a variable as Property, which we can easily do in UserControls. To make it more clear, let's say I want to pass a variable to ma usercontrol, on the basis of which my website navigation selects and deselect current menu in the header. But using include how can i perform this ??
Prashant
Check out Smarty. This sort of thing is possible using it's templating engine. It will be a bit of a paradigm shift for you though.
Matthew Vines
+1  A: 

A second answer which is a bit harder to explain in detail here, is to build your own 'UserControl' classes. The Classes would have the neccessary properties that you require, and Would have a RenderHTML() method that would output the control to the screen.

Matthew Vines
hmmmmmm... looks cool.. we can apply this... but then for every control we have to make separate Ucontrol class ??
Prashant
I would use an interface to ensure that all your control classes behave in the same manner. But yeah you would have to create a separate class for every user control you wished to make. But this is the same in Asp.net. All the normal inheritance rules would apply, where one ucontrol could inherit from another, etc.
Matthew Vines
Yes, for every different control we have to create a UserControl in asp.net too and same can be applied to PHP class method. :)
Prashant