1

in asp.net there are controls like grideview, menus, .....

how to develop a web control in php like an editor [html, jscript, ajax calls to server] it is used repeatedly and the improvement of control will be better if we can separate it?

Charles
  • 50,943
  • 13
  • 104
  • 142
ahmedsafan86
  • 1,776
  • 1
  • 26
  • 49

3 Answers3

3

First of, be aware that in PHP "control" and worse, "controller" means completely differently.

I've not used ASP, but I've used Delphi extensively, as well as Delphi for PHP, which does the same thing you are asking about (yes, in PHP).

Basically, you need some sort of framework to build this stuff on, as Johann said, you might want to use MVC, but you don't really have to.

Example of such a system (without MVC):

class TControl {
    public $width=0;
    public $height=0;
    public $name='ControlN';
    public function render(){
        echo '<div style="width:'.(int)$this->width.'px; height:'.(int)$this->height.'px;"/>';
    }
}

class TLabel {
    public $caption='Label';
    public function render(){
        echo '<div style="width:'.(int)$this->width.'px; height:'.(int)$this->height.'px;">'.htmlspecialchars($this->caption,ENT_QUOTES).'</div>';
    }
}

$label=new TLabel();
$label->width=200;
$label->height=24;
$label->caption='My label!';
$label->render();
Christian
  • 27,509
  • 17
  • 111
  • 155
  • +1 _THANK YOU_ for pointing out Controller and Controls. Apples to oranges – JohnP Mar 29 '11 at 07:37
  • thanks i use zend framework, i want to add control for editing articles[always tinyMCE] this is frequently used in many pages i want to encapsulate it in a class like what you did. but it has javascript interaction how to manage javascript in my control? – ahmedsafan86 Mar 29 '11 at 08:42
1

In PHP you write all 'controllers' in plain HTML.

If you want 'controllers' that you use more often, then make functions for them, f.ex;

function list($array)
{
   $output = '';
   foreach($array as $item) $output .= "<li>$item</li>";
   return "<ul>$output</ul>";
}

Now you got an 'list controller'.

Knarf
  • 1,282
  • 3
  • 12
  • 31
  • Not in C#, C# got its own 'controllers' that you use. C# try to mimic Windows programming with 'controllers'. – Knarf Mar 29 '11 at 07:14
  • LOL you can mimic it in PHP as well – Your Common Sense Mar 29 '11 at 07:15
  • Could you give an example on how you 'mimic it in PHP'? – Knarf Mar 29 '11 at 07:18
  • you just code it as a library or plugin and include it in your project. C# controls are just code. It's just a difference in environment – JohnP Mar 29 '11 at 07:20
  • 3
    If you use a **framework** that comes with something like "controllers", you have the same thing as well. *C#* doesn't have controllers, the *.NET framework* does. PHP doesn't have controllers, but the Zend Framework or CodeIgniter or CakePHP have something analogous. – deceze Mar 29 '11 at 07:22
  • You can mimic it indeed in PHP, but PHP doesn't have build in functions that try to mimic it, C# does actually try to mimic it, they don't want developers to even look at the HTML, they do it all for you. In PHP you have to actually know HTML. – Knarf Mar 29 '11 at 07:23
  • Good said @deceze. That would probably be the answer to Ahmed safan question. **Use an framework** – Knarf Mar 29 '11 at 07:24
  • 1
    @Knarf @deceze The OP and Knarf are talking about Controls and deceze is talking about Controllers. Apples to oranges. – JohnP Mar 29 '11 at 07:36
  • Knarf - I disagree; PHP by itself does require this, but if you try Delphi for PHP, you may not need to learn HTML at all. JohnP is right. – Christian Mar 29 '11 at 07:37
  • @John Typo! s/controllers/controls/. Got carried away by @Knarf's typoed comment. I'd call them "helpers" to begin with. – deceze Mar 29 '11 at 07:40
1

ASP.net tries to mimic Windows Forms, this allows Windows Forms users to quickly get a website up and running. And as it mimics Windows Forms they created several preloaded user controls that you can insert into a web page. All these do is produce the styles, JavaScript and the HTML necessary to show the control on the client browser with your configured options. You could try to produce a similar effect in PHP , with .php that you include. For example creating a button.php and then including it where you want the button to be displayed.

But for simplicity I find that the MVC approach to development with PHP and a Framework is much more cleaner.

Johann du Toit
  • 2,609
  • 2
  • 16
  • 31
  • True, but as the user is trying to use the ASP.NET controls in PHP I thought i'd give them another way to structure their application. – Johann du Toit Mar 29 '11 at 07:28