Creating Components in PHP
Moderator: General Moderators
Creating Components in PHP
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
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:
Or something more abstract:
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:
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.
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);
}
?>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;
}
}
}
?>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');
?>PS: please don't double post. The advanced forum is the correct place for this topic.
Last edited by McGruff on Wed Aug 10, 2005 5:51 pm, edited 2 times in total.
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
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
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
-
Cruzado_Mainfrm
- Forum Contributor
- Posts: 346
- Joined: Sun Jun 15, 2003 11:22 pm
- Location: Miami, FL
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas