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
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

 

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.