Sign in to follow this  

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

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this