PHP and declaration of types ... (Re: [PHPLIB] WARNINGS) From: Alexander Aulbach (ssilk <email protected>)
Date: 03/06/00

On Mon, 6 Mar 2000, Lukas Zapletal wrote:

}Hello, I`m using phplib 7.2 and i usually set PHP warning level to
}highest (all warnings), but when I include phplibs, there are two
}warnings:
}
}Uninitialized variable in sessions.inc at 96
}Uninitialized variable in sessions.inc at 396
}
}...and then, of course :)
}
}Oops, cannot set cookie....
}
}So, I have to fix it or set warning level back to 7.
}
}Is it my fault or is it a (small, but it is ;) bug?

It's of course your fault. :-)

No, it seems to be a bug, cause most users program without check for
uninizialized vars turned on. Would be good to commit your work to CVS.

But just some ideas on it, cause just last week I thought about this
problem. And my ideas ended at the problem, that it is not possible, to
declare vars in PHP.

In my opinion this is a lack in PHP, cause we
(web-application-programmers) need a check for such unizialized vars
BEFORE running the program. It is a no no for every application, that it
brings such simple errors during runtime; the users of the application
will wonder very much about such things. That's the main reason I think,
why to put this check OFF...

But the only way to test it in detail is to put it on and to test it. :)
But in the view of the programmer this is not a very nice method to test a
program, it takes to much time and you cannot test everything in a
program, which is a fact.

Other programming languages support this problem by introducing
type-declarations.

So I would present now my ideas to a declaration-mode into PHP. :)

The main idea is, not to restrict the programmer in using it or not - so I
called it declaration-*mode*. This means, that it is nowhere a must of
using it. But if once used in a function, program or object (?),
*everything* must be declared. This makes it possible to write code very
fast and at the end, while testing it you can introduce the declarations
and will hopefully find some more errors. But at this stage of programming
this is a need and not a must.

This also prevents newbies in entangling to declarations and of course it
makes the language downward compatible.

For critical functions and programms you then have to declare every little
variable. And you can declare them with a fixed type. E.G.

<?php

DECL $var1, $var2, $var3=10;

$x=1; # produce warning while starting the prog cause not
                    # declared (once used inside a function it will check
                    # everything.

$var1=1; # ok

print $y; # produce also warning while starting

print $var2; # ok, will print out nothing, like in PHP
print isset($var2); # will print out 0, cause false
print $var3; # will print out 10

DECL $var4=$var1+$var3; # Why not? But this makes it difficult to
                        # find out, if a var has a valid type,
                        # so perhaps a bad idea...

DECL $string:str , $integer:int, $real:double, $array:array, $obj:object;
## alternative I could think this equivalent format:
DECL (str)$string, (int)$integer, (double)$real; # etc.
## but I think this is more useless typing

$string=1; # produce warning, cause result is int and is not
                    # string

$string:int=1; # redeclarates $string

print $integer; # will print out "0", cause it will take the intval

$hugo:int=1; # same as
DECL $hugo:int;
$hugo=1; # cause the programmer makes an implizit declaration

$var4:int=$var1+$var3; # this should be allowed everywhere
                       # cause the programmer says to the language:
                       # "hey, I've thought to the problem of declaring
                       # $var4, and this is the result of my thoughts"

# and very new:
DECL $type:type;
$type="array";
DECL $array:$type; # simple, eh? see down! :)

# You also can declare own global types
DECL aulint:int; # :)

# Some questions:

$real = 1; # Warning or not? Cause 1 is integer, 1.0 is double
$int=$int+$real; # Warning or not? Cause the correct result is double,
                    # not int

DECL $a, $b=1.0, $c:int;

$a=$b+c; # $c is undefined, what happens?
$c=1;
$a=$b+$c; # What is the resulting type?

DECL aulint:string; # Warning or silently overwritten?

?>

Once used in a function or in the main program, all vars will be checked.
But I wouldn't introduce this to every function or programm, cause it
makes for example *no sense* to declare every variable you get from a
HTML-form (the current way to program such things are really the best). As
I said, I would only use this mechanism for more or less critical
functions, objects, and some special programs.

BTW, functions. It would be a good idea to declare the vars also:

<?php

function bla ($myval:int) : str {
        return(strval($myval));
}

$hugo:str=bla('1'); # warning, cause operator is not int

$hugo:str=bla(1);
# here this is a difficult question: is it also correct to write

$hugo=bla(1);
# ??? Why? Cause the declaration is already made in the function
# and if you change it the type of $hugo will also change.
# But the side effects?

?>

Some more ideas: Would be fine to declare own types of vars (as said
above). This is just a new basic type of var in PHP. I mean especially
associative arrays, which are an equivalent to an record in other
programming languages.

<?php

DECL $bla:array:
                color:str,
                width:str,
                height:str,
                size:int
               ;

$bla=ARRAY(
           color=>'#AAAAAA',
           width=>'100%',
           height=>'10'
          );

$bla[hair]='blond'; # warning, cause not declared
print $bla[name]; # also warning, cause can have no value

# Then it would be a nice idea to make a function, which can do
# a check for unset values on arrays:

for ($i=issetarray($bla); list(,$val) = each($i); ) {
        print "UNSET: $val";
}

## To declare an array of unknown numbers of arrays you need to do so:

DECL $blubb:array:
                  []:
                     color:str,
                     width:str,
                     height:str,
                     size:int
               ;

?>

PHPLIB can support this in the following way for tables: Query-class can
create an type, which then can be declared.

<?php

include("query_sql.inc");

$db=new Query; # perhaps an equivalent writing would be:
DECL $db:object:Query; # cause a class is in this case of view nothing
                       # else than a type

$db->Host="localhost";
$db->Database="test";

$mytype:type=$db->create_table_type("auth");
# this returns a type like
# array:
# user:str,
# passwd:str,
# logged_in:int,
# passwd_mismatch:int
#
# just the definition of the table "auth" in the database "test"
# in the view of the limited types of PHP
DECL $bla:$mytype;

# An alternative form could be:

DECL $bla:$db->create_table_type("auth");

# which seems to me a very cool programming construct... :-)

?>

Some last suggestion: It could be a good idea if misuseing associative
arrays for an record, to define arrays, where the associative keys are
caseless (I dosn't matter if I write $a[HUGO] or $a[hugo]).

And some last words: Of course this is very difficult to implement. And I
don't need it to be implemented in PHP4. But perhaps someone is willing to
do it so for PHP5?

And the future: Thinking about the possibility to declare subtypes. For
example date is a subtype of a string. So it could be written as
<?php
DECL $today:string/date;
?>
This goes more or less in the direction object orientation, but also
makes it possible to handle other types of data, for example
<?php
DECL $picture:image/jpeg;
?>
...

-- 

SSilk - Alexander Aulbach - Herbipolis/Frankonia Minoris - (0931)22032

- PHP3 Base Library Mailing List. Send messages to <phplib <email protected>>. To unsubscribe, send "unsubscribe" to <phplib-request <email protected>> in the body, not the subject, of your message.