Tag: PHP

2010-06-08

Book Review: CodeIgniter 1.7 Professional Development

by Charles — Categories: OOP, PHP — Tags: , , Leave a comment

I was recently provided a review copy of CodeIgniter 1.7 Professional Development by Adam Griffith (Packt Publishing). It claims that it will help the reader “Become a CodeIgniter expert with professional tools, techniques and extended libraries.” As someone who has used CodeIgniter and found it to be very useful, I was looking forward to learning more and becoming a CI power user. However, I cannot say that my wish was fulfilled.

While some of the second half of the book introduced me to a few CI features I did not know about or at least had not yet used, most of the book was either about things I already knew or things I didn’t really care much about.

Most of the first half of the book essentially reiterated things you can find in the CI documentation, and that documentation is one of the strong points of CI. Also, some of the organization of the material was odd, such as detailing the image manipulation library before discussing a basic functionality such as session data handling. Some of the code samples seemed to be less than optimal as good examples of object-oriented PHP, such as the large switch/case block in the “Rest” class in Chapter 8.

My overall response to this book is therefore that it might be of use to intermediate-level PHP users who are new to CodeIgniter (with the caveat that it is not necessarily a good source for learning good OOP practices), but the experienced CodeIgniter user probably will find little in this book to make it worth purchasing. I’d probably only give it 2-1/2 stars out of 5.

2010-01-25

Beginners’ Corner: Variable Function Parameters

by Charles — Categories: Beginners' Corner, PHP — Tags: , , , Leave a comment

Sometimes when defining a function in PHP, you find that there may be cases where you do not want to specify all the possible parameters when calling it. This could be because you want it to use a default value, or that in certain cases it does not logically apply. If you find yourself in such a situation, probably the first thing you should do is ask yourself if two (or more) separate functions should be defined, instead. (Perhaps one function would call the other internally?)

Assuming you want to stick with one function, the most commonly used approach is to define a default value for one or more parameters in the parameter list. A parameter is assigned such a default value via the = operator, just as you would use it to assign a value in the function body.

function foo($bar='World') {
   echo "Hello, " . $bar;
}

When the function is called at run-time, if that parameter is not included in the call, then the default value will be used; otherwise the supplied value is used.

foo(); // Hello, World
foo('Joe'); // Hello, Joe

If you want your function definition to include both required and optional (default) parameters, the required parameters must come first in the list.

function foo($req1, $req2, $opt1='something', $opt2=null) { }

In the preceding example, the use of null can be convenient in that PHP treats a null variable as “not set” by the isset() function, allowing you to do something like:

function foo($bar=null) {
   if(isset($bar)) {
      echo $bar;
   } else {
      echo "Nobody home";
   }
}

Another approach which can be quite powerful in some ways, but for which I’ve really not run into much use myself, is the use of variable length argument lists. Essentially, the function prototype would have no parameters specified, then you use the func_num_args(), func_get_arg(), and/or func_get_args() functions to determine how many arguments were supplied and then decide what to do with them.

function test()
{
   $numArgs = func_num_args();
   switch($numArgs) {
      case 0:
         return false;
         break;
      case 1:
         echo "Hello, " . func_get_arg(0); // args start numbering at zero
         break;
      case 2:
         echo func_get_arg(0) . ", " . func_get_arg(1);
         break;
      default:
         echo "Here are the args:<br /><pre>";
         print_r(func_get_args());
         echo "</pre>";
   }
}

Lastly, another approach is to pass an associative array as the function parameter. You can then test for specific array keys to decide what to do.

function test($args)
{
   if (!is_array($args)) {
      user_error('Oops!');
      return false;
   }
   if (isset($args['name'])) {
      if (isset($args['verb'])) {
         echo $args['verb'] . ", " . $args['name'];
      } else {
         echo "Hello, " . $args['name'];
      }
   }
   return true;
}

A downside of this last method is that the function definition’s parameter list gives no clue by itself as to what array keys are expected/allowed. This can make it tricky to use and maintain, as the coder is at the mercy of the comments and documentation, or else is forced to read the entire function definition to figure out the possibilities.

2009-10-27

Beginners’ Corner: Learning Object-Oriented PHP

by Charles — Categories: Beginners' Corner, OOP, PHP — Tags: , , , 2 Comments

I often see PHP newbies (and even not-so-newbies) who are confused by the world of object-oriented programming (OOP). At least part of this confusion results from the vast majority of introductory books and tutorials for PHP beginning by teaching procedural programming techniques, treating OOP as an “advanced” subject with a chapter or two at the end. Thus the student is exposed to a bunch of new syntax concerning classes and objects with, at best, a very brief explanation as to “why” that syntax should be used. A purely object-oriented language such as Java or Ruby, on the other hand, will by its nature require that beginners’ resources more fully address the theory of OOP.

I am not going to claim to be an OOP guru, but I can point you to a few resources I have used to become reasonably proficient — but still learning! — with PHP classes and objects. First I would point you to an on-line tutorial at Sun.com: “Lesson: Object-Oriented Programming Concepts“. While the (relatively few) examples in these tutorials are in Java, the syntax is quite similar to that of PHP, and the lessons they illustrate are easily transferred to PHP. If you want to work with those examples yourself in PHP, I would recommend taking a look at a couple PHP tutorials at IBM.com: “Getting started with objects in PHP V5” and “Advanced PHP V5 objects“.

Once you’ve gotten past the basics, to see how things can work together as an OOP whole, I strongly suggest you get your hands on Matt Zandstra’s book, PHP Objects, Patterns, and Practice. Reading this book is what ultimately made objects “click” in my mind. After reading it I felt much more comfortable with the underlying concepts along with the specific techniques for applying them in my PHP projects.

2009-09-17

Book List Project: Using the “Template” Pattern

by Charles — Categories: PHP — Tags: , , , Leave a comment

As part of my Book List project I am making use of the Template design pattern for my CodeIgniter database table models. The idea is that an abstract class contains the common processing needed by each class that will extend it, and it will have some number of abstract methods which essentially force each child class to set variables that differentiate each child class — in this case the database table and column names for the specific model class.

(more…)

2009-09-17

Komodo Edit 5.2.0 Released

by Charles — Categories: General, PHP — Tags: , , , Leave a comment

Download Komodo Edit 5.2.0

The 5.2.0 version of Komodo Edit has just been released. In addition to some bug fixes and UI features, the release notes indicate that support for PHP 5.3.0 has been added, including code completions for name spaces and syntax coloring of latest keywords (“namespace”, “use”, “as”, etc…).

(FYI, this is the editor I’m currently using, but who knows which I’ll be using a few months from now.)

2009-09-08

Upgrade WordPress to Avoid Potential Exploit

by Charles — Categories: General, PHP — Tags: , Leave a comment

If you run any WordPress blogs, be sure to upgrade your software to the latest version (2.8.4 as of this writing) to avoid an exploit which may place your blog or web site at risk. See the article at “Lorelle on WordPress” for more info. Either use the upgrade link on your WordPress blog’s control panel, or download and install the latest stable version.

2009-08-02

Komodo Editor: PHP Code Beautifier

by Charles — Categories: PHP — Tags: , , , Leave a comment

So I’ve been playing around with Komodo Edit (the free editor available from the people who make the not-free Komodo IDE), and one thing I found missing was a code beautifier (or automatic formatting, if you prefer). It’s not a feature I use a lot, but can be really useful when importing code from someone else.

So after a bit of Googling, I found this article: PHP Code Beautifer Macro+Script. I followed the instructions to install the PEAR PHP_Beautifier package, saved the provided phpbeautifer.php script in my PHP directory, installed the macro file into the Komodo Edit “Toolbox” and edited it so that it pointed to where I’d put the PHP script (using double back-slashes for the directory separators since I’m on Windows), and it worked! All you have to do is double click the phpBeautifier icon in the Toolbox window, and it formats the currently active file.

The only change I’ve made so far is to edit the phpbeautifier.php file to remove the addfilter() instruction to insert a newline before functions, as that messed up classes with any access keywords before the function keyword. I also changed the indention to 3 spaces, and added the filter for PEAR styling.
(more…)

© 2012 PHP Musings All rights reserved - Wallow theme v0.46.4 by ([][]) TwoBeers - Powered by WordPress - Have fun!