Archive

Archive for February, 2009

PHP5 simple type hinting

February 16th, 2009 No comments

The symptom error messages may include:

must be an instance of string, string given
must be an instance of integer, integer given

The cause:

“Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn’t supported.” — http://ca.php.net/oop5.typehinting

Summary:

You can’t type hint STRING or INT, at least as of PHP 5.2.8, the current release at the time of this post.

Categories: PHP Tags:

Override php.ini to turn on error reporting, and display errors

February 13th, 2009 1 comment

If you’re server doesn’t allow you to show error in php try the following lines at the near the top of your script.

Try this:

<?php
// Report all PHP errors
error_reporting(E_ALL);
ini_set("display_errors", 1);

If it still doesn’t show up, the web server daemon may not have write permissions to the default logging directory.

Categories: PHP Tags: , ,

Excellent post on programmer mistakes

February 8th, 2009 No comments

Great posting of stupid things programmers do… The * stupidest things I’ve done in my programming job

In all honesty, I think most programmers should make these kinds of mistakes on their own, and then learn from them.

One that does not understand the wrong, cannot become better.

To extend the post, specifically for PHP:

1. ORM

One should use Doctrine, or propel.  I personally favour Doctrine.

2. EAV

Doesn’t need to be PHP specific.

3. Database Access

PDO with Doctrine, can’t really go wrong…

4. IDE

Use an PHP IDE, there are lots out there now.

Eclipse PDT, NetBeans PHP, PHPEditor, Aptana… etc…

I currently favour NetBeans.  I have tried Eclipse PDT, Aptana, NetBeans… and I’m currently using NetBeans, and loving it.

5. Transactions

Just use them.  This reminds me to clean up, the data injector to use  transactions.  I know I can… I don’t know why I didn’t.

6. Prepared Statements

Prepared statements in PDO, or subsequently Doctrine, is trivial.

PDO example:

<?php
// the following statements has question marks inside the query...
// meaning, that those will be replaced with values later.  Think of the command as a combination of sprintf, and mysql_real_escape_string, together... but smarter.

$query_string = 'select * from tableA where id = ? and column_a = ?';

$this->prepared_statement = $this->DBH->prepare($query_string);

$this->prepared_statement->execute(array(123, "some random value, but will auto quote everythign for me even if it has \" and ' inside the string"));
$results  = $this->prepared_statement->fetch();  // or
$results2 = $this->prepared_statement->fetchAll();

Doctrine is an ORM, so if you’re using Doctrine, basically everything is prepared for you.

7. Business Logic

LOL, sorry… this one makes me laugh, as code at work has this type of “design” (or lack of) in legacy code.  This legacy code is less than a year old.  Thank you Botched Code!

Categories: General Programming Tags: