Re: Re: "use strict" or similar in PHP?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2009-03-01 at 08:36 -0500, tedd wrote:
> The OP asked:
> 
> >Is there in PHP something like "use strict" from perl? I find it pretty
> >annoying to need to run script over and over again just to find out that I
> >made typo in variable name.
> 
> And I've been waiting for an answer myself, but I haven't seen one.
> 
>  From what I remember, in perl if you use "use strict;" it requires to 
> to define your variables (my) before using them. If you make a 
> variable typo in your code, then you'll trigger an error when you try 
> to run it.
> 
>  From what I've seen of php, even with using strict error reporting, 
> you can do that all day long without generating an error.
> 
> So the answer appears to be "No, you can't do that in PHP." Is that the answer?

You can do anything you want... :)

<?php

class StrictProps
{
    private $___init = true;

    function StrictProps( $props )
    {
        $propsList = func_get_args();

        foreach( $propsList as $props )
        {
            if( is_array( $props ) )
            {
                foreach( $props as $prop => $init )
                {
                    $this->{$prop} = $init;
                }
            }
            else
            {
                $this->{$props} = null;
            }
        }

        $this->___init = false;
    }

    function __set( $name, $value )
    {
        if( !$this->___init )
        {
            die( 'Attempt to set non-existent property: '
                .get_class( $this ).'->'.$name."\n" );
        }

        $this->{$name} = $value;
    }

    function __get( $name )
    {
        die( 'Attempt to get non-existent property: '
            .get_class( $this ).'->'.$name."\n" );
    }
}

//
// Declare strict properties...
//
$my = new StrictProps( 'foo', 'fee', 'fii' );
print_r( $my );

//
// Declare and intialize strict properties...
//
$my = new StrictProps( array( 'foo' => 'Foo1', 'fee' => 'Fee1' ) );
print_r( $my );

echo $my->foo."\n";
echo $my->fee."\n";
echo $my->blah."\n";

?>

Cheers,
Rob.
-- 
http://www.interjinn.com
Application and Templating Framework for PHP


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux