Page 1 of 1

Creating Components in PHP

Posted: Mon Sep 22, 2003 1:58 pm
by saeen
Hi im Jd..totally new to PHP...just need to ask one small thing may b ull feel its a stupid Q but can u please tell me how can i create COMPONENTS in php ... like r the components simple php files or anythih else please please please tell me...thanx

Posted: Mon Sep 22, 2003 2:53 pm
by McGruff
Being self-taught I couldn't say how a "real" programmer would define components so the following is just the way I look at things.

A component could be anything from a small user-defined function to a whole object which is tuned to carry out a specific task. Abstract components can apply to a wide variety of uses while still being tightly defined.

A php highlighter would be a single-use component:

Code: Select all

<?php
function phpHighlighter(&$string) 
{
    $replace = null;
    
    preg_match_all("#\[php\](.*?)\[/php\]#si", $string, $matches);

    $i = 0;
    foreach ($matches[1] as $value) {

        $replace[$i] = '</p><div class="php">' . highlight_string('<?php ' . $value . '?>', 1) . '</div><p class="body">';
        $i++;
    }    
    $string = str_replace($matches[0], $replace, $string);
}
?>
Or something more abstract:

Code: Select all

<?php
// apply $function to all elements in an array of any depth; $function can only take a single value as an arg (which will be the current array element) and $function must return a value (replaces the element after processing by the function)
function arrayIterator(&$array, $function) {

    foreach ($array as $key=>$value) {
        
        IF (!is_array($value))
        {
            $array[$key] = $function($value);
        
        } ELSE {

            arrayIterator($value, $function);
            $array[$key] = $value; 
        }
    }
}
?>
The goal I guess is to create a framework (or seedwork http://www.phppatterns.com/index.php/ar ... w/76/1/11/) of re-usable, tried and tested components thus saving time when creating a new program. There's some open source stuff to draw on for inspiration such as the eclipse library http://www.students.cs.uu.nl/people/voo ... /index.php
or ADOdbhttp://php.weblogs.com/. I hear bad things about PEAR though.

Refactor ruthlessly creating lots of little functions which carry out discrete tasks rather than one big plate of spaghetti. Look for blocks of repeating code or patterns in your scripts, and wrap them up in re-usable component functions or objects. The arrayIterator for example describes a common pattern of "do something to all the elements in an array". It would simplify the code in the calling script down to, for example:

Code: Select all

<?php
arrayIterator($array, 'mysl_escape_string');
?>
These are just simple fns: OOP and patterns is ultimately the way to go. http://www.phppatterns.com/index.php/article/archive/1/

PS: please don't double post. The advanced forum is the correct place for this topic.

Posted: Wed Sep 24, 2003 6:24 pm
by Cruzado_Mainfrm
well actually in php there are no components but objects also called classes, objects are like components in a sense(logic perspective)
i think :D, also u might want to learn about what 'classes' are and 'object oriented programming'

Posted: Wed Sep 24, 2003 8:20 pm
by fractalvibes
I tend to think of Components in an ASP sense, that is COM or COM+
objects that are compiled DLLs written in, say, VB or C++. None of the scripting langauges such as PHP or ASP has this capability.

If you are talking about chunks of reusable code - sure, you can create functions, objects and such in PHP, or even Javascript (client side).

fv

Posted: Wed Sep 24, 2003 8:44 pm
by Cruzado_Mainfrm
for me components are like textboxes, richedits, buttons, pagecontrols, progressbars, as in VB, delphi, c++, etc.

Posted: Fri Sep 26, 2003 10:26 pm
by fractalvibes
Sure, a component could be anything from a ASP.Net datagrid or label to a DLL that converts Word Docs to PDF.

Perhaps saeen can more precisely define the context of what he is looking for in terms of `Component`? Components can be many different things - a tire is a component of my vehicle....

fv