Jump to content

Defining Variable Types


TomTees

Recommended Posts

Is it possible to define a variable's type at the start of your code?

 

I believe you can "cast" a variable, but can I do something like this...

 

name     string;
age        integer;
gender   string;
children  array;
single:    boolean;

 

I know some people say PHP is better suited for the web because it is loosely-typed, but I'm anal-retentive and like my structure!!  8)

 

 

 

TomTees

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/214982-defining-variable-types/
Share on other sites

Nope, you can't do that.

 

Semi-related: You can do type-hinting in PHP 5.1+ in object methods: http://php.net/manual/en/language.oop5.typehinting.php

 

edit: You could also check out the experimental SPL Types extension: http://pl.php.net/manual/en/book.spl-types.php

Nope, you can't do that.

 

Semi-related: You can do type-hinting in PHP 5.1+ in object methods: http://php.net/manual/en/language.oop5.typehinting.php

 

edit: You could also check out the experimental SPL Types extension: http://pl.php.net/manual/en/book.spl-types.php

 

Are there any plans to support the information in the links above in any future versions of PHP?  (I would have thought PHP 6 would include that?!)

 

Also, what if I did this...

 

$name = 0;
$name = (string) $name;

 

 

OR

 

 

// Define bogus variables
$a=$b=$c=$d=0;
$s = 's';
$a = array();
$b = TRUE;

// Cast bogus variables into desired Data-Types
$name = (string) $a;
$age = (integer) $s;
$gender = (string) $b;
$children = (array) $c;
$single = (boolean) $d;

 

I realize that may seem like a nonsensical approach, but it would allow you to have defined data-types, right?

What do you think?

 

 

 

TomTees

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.