简体   繁体   中英

Can I focus php to type the variable class in php?

In Java, everything object, I know which type it is, for example... I will doing something like this:

MyObject aObject = new MyObject();

I always know that the "aObject" is MyObject...

but PHP, I use this

$aObject = new MyObject();

I don't know what is the "aObject" was, I try to doing this, it gives me error:

MyObject $aObject = new MyObject();

It makes me feel very focus on that, any suggestions? Thank you.

You might want to read PHP: Type Juggling , and PHP: Type Hinting on php.net.

You can make great use of comments like that

// data type is MyObject
$aObject = new MyObject();

That is, unfortunatley for you, how PHP works. There are no strict data-types in PHP, what so ever. It is something that you have to grow into to be a PHP-programmer.

No, in PHP you can do Type juggling:

$aObject = new MyObject();
$aObject = new UnrelatedObject();

is perfectly valid PHP. It's not necessarily a bad thing though. It has both pros and cons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM