Home » Php » typechecking – Force PHP to error on non-declared variables? In objects?

typechecking – Force PHP to error on non-declared variables? In objects?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Is there any way to force PHP to blow up (error, whatever) if I misspell a variable name? What about if I’m using an instance of a class and I spell the name of a variable wrong?

[I know that I should just get used to it, but maybe there’s a way to enforce name checking?]

Thanks!

Edit: Sorry, that wasn’t very specific. Here’s the code, and I would like to get two errors. Right now I only get one (for the last line).

error_reporting(E_ALL|E_STRICT);
class Joe {
    public $lastName;
}

$joe = new Joe();
$joe->lastNombre = "Smith";
echo "And here it is " . $jose;
How to&Answers:

Here is something I whipped up really quickly to show how you can trigger errors when something like that happens:

<?php

error_reporting( E_ALL | E_STRICT );

class Joe {
    public $lastName;

    public function __set( $name, $value ) {
        if ( !property_exists( $this, $name ) ) {
            trigger_error( 'Undefined property via __set(): ' . $name, E_USER_NOTICE );
            return null;
        }
        $this->$name = $value;
    }

    public function __get( $name ) {
        if ( !property_exists( $this, $name ) ) {
            trigger_error( 'Undefined property via __get(): ' . $name, E_USER_NOTICE );
            return null;
        }
        return $this->$name;
    }
}

$joe = new Joe();
$joe->lastNom = "Smith";
echo $joe->lastNom , "\n";

?>

Answer:

From the PHP docs on error_reporting:

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

Answer:

You could try:

error_reporting(E_ALL|E_STRICT);

EDIT: OK, I’ve now read the updated question. I think you’re out of luck. That’s valid in PHP. Some say it’s a feature. 🙂

Answer:

You can use this in your code:

error_reporting(E_ALL);

or this in php.ini

php_error_reporting=5

http://us2.php.net/error_reporting

Answer:

Setting error_reporting to include E_NOTICE may help somewhat. It has a tendency to display a notice/error whenever you use a undefined variable(note that it doesn’t stop the execution).

Answer:

Know this is an old post – but stumbled across it looking for a solution.

This is my final solution:

I have an abstract class that my classes extend that throws an error on magic __get and __set.

It’s nasty – but it works a treat!

It’s similar to suggestions above – but have also added ‘final’ to the method declaration to stop people overwriting it.

We also use a code sniffer to catch these – but I wanted to get my error messages ‘live’ as I coded, instead of having to wait for a report from the sniffer.
(Also people can ignore sniff reports!)

Sample code below

   <?php
/**
 * Abstract
 * Some default Abstract functions that everything should extend
 *
 * @package    example
 * @subpackage lib
 */

/**
 * Class mh_abstract
 * Some default MH Abstract functions that everything should extend
 *
 * @package    example
 * @subpackage lib
 */
abstract class lib_abstract
{
    /**
     * Throws an error if php magic set is called
     *
     * @param string        $key
     * @param string|object $value
     *
     * @throws Exception When trying to set a new class property
     */
    final public function __set($key, $value)
    {
        // get if the thing we are trying to set is a object
        if(is_object($value))
        {
            // if so let's just report it's name, not the full object
            $value = 'object:'.get_class($value);
        }

        // throw the error!
        throw new mh_lib_error_dev_unknownClassProperty('Tried to set new property '.$key.' with value '.$value);
    }

    /**
     * Throws an error if php magic get is called
     *
     * @param string $key
     *
     * @throws Exception When trying to set a new class property
     */
    final public function __get($key)
    {

        // throw the error!
        throw new mh_lib_error_dev_unknownClassProperty('Tried to get new property '.$key);
    }
}

Answer:

To catch the undeclared property you can set your _ _Set() magic method to catch them

function __set($sKey, $sValue)
{
   // any setter data should be *returned* here

   // NOTE: this function will only get called if the property is not
   // publicly accessible.
   trigger_error("Non-existing property name ".$sKey, E_USER_WARNING);
}