[web] Tips for a C++ Programmer programming in PHP?

Started by
4 comments, last by markr 17 years, 11 months ago
I don't mean to stir up any controversy (and I'm not sure how I could), but I have a simple question: </safeguard> Does anyone out there have any tips for programming in PHP after programming in C++? I know that PHP is heavily based off of C++, but every time I code in PHP, I simply feel dirty, especially when using MySQL queries (searches, filtering after the query, etc). Are there any suggestions for better handling of PHP to feel less 'awkward', such as arrays, strings, and classes?
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
Advertisement
Tip: forget about C++ in the context of PHP, learn PHP, and become a PHP programmer.
Ach.
Well, I guess that'll be it.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
I agree. The only thing PHP and C++ have in common is curly braces, a few basic control structures and operators, and a very tiny little few function names.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

PHP isn't really based on C++, it's based on C - but via Perl, which in turn was based on shell scripts - hence $ in front of variable names.

If you're writing OO, bear in mind that scoping doesn't work like in C++, i.e.

$this->x
and
$x

Are completely different, $x will always be treated as a local (unless you've done global $x).

Read up about variable scoping, because it confused me when I started PHP.

Basically, PHP has function scope by default - anything which isn't taken from somewhere else or qualified, is a local variable.

PHP does *NOT* have lexical scope, like C++. A closed curly brace does not cause locals to go out of scope unless it's the end of a function. So

function Something($blah) { if ($blah) {   $x = 42;} else {  $x = 99;}// $x still in scope}


Mark
Tips for anyone programming PHP:

- Use the latest version of PHP *if possible*. Don't worry about backwards compatibility. PHP is NOT backwards compatible generally.
- DO enable all errors and warnings with error_reporting
- DO write an error handler which shouts, screams, kicks and throws a massive fit as soon as it sees even an E_NOTICE
- DO log errors etc

The problem is, a lot of mistakes cause E_NOTICEs. If you disable them (as they are by default), they are completely silent, so you won't see them at all and can get away with blue murder.

The other problem is, a lot of stuff generates an E_WARNING if it has failed completely - e.g. opening a file, connecting to a database. In practice, there is rarely any need to continue running code using the "Keep running headlong into oblivion" school of error non-handling.

So I really recommend you write an error handler which terminates the script as soon as it sees a (non-suppressed) E_WARNING. Another option is to throw an exception which the caller can catch if they *really* want to continue.

Mark

This topic is closed to new replies.

Advertisement