ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is "Weak Typing" in PHP and how to handle it with exception handling?

user-image
تم إضافة السؤال من قبل shahbaz khan , Senior PHP Web Developer , IBM Systems (US)
تاريخ النشر: 2017/01/03
مستخدم محذوف‎
من قبل مستخدم محذوف‎

This is the best simple explanation of weak typing in PHP:

source

A loosely typed language such as PHP is a language that does not require you to declare a variable type when declaring a variable.  What does that mean?  In PHP when you declare a variable, you can with no need to worry about what kind of data will be stored in that variable.  I could make a new variable called $string and then assign it an integer value.

Languages such as C# and others are strictly typed, meaning that variables MUST declare type at the time the variable is declared, and that type is strictly enforced.  This means if I declare a new variable I have to specify a data type to enforce that variable.   Once I have declared a variable as being one type, the data in that variable must remain of that type unless the type is ‘Cast’ to a new type. Type Casting is the process of converting a variable of one type to another.  Seems like a lot of work huh?  It can be, but it has its benefits as well.

When you code in PHP, you don’t have to worry about what kind of data is coming in and out of your variables if you don’t want to.  This does not mean you shouldn’t though!  One of the biggest problems with PHP applications is that programmers don’t organize their data well, and applications run buggy.  Having variable be loosely typed can lead to all kids of unexpected run-time errors if you are not careful.

On the flip side, not being constrained to a specific type can add flexibility, and allow for far more dynamic capabilities.   PHP is one of the most flexible web programming languages I have worked in.  Perl is another one.  The flexibility provided with a loosely typed language can be golden to programmers who are aware of the challenges they also present.  If you can organize your data well, and write your own constraints to fit within your program, you can do amazing things with PHP.   If you don’t however, you can produce some very buggy code.

The Draw Back:

The biggest problem with loosely typed languages is the opportunity it gives programmers to write very bad code!  I myself wrote a lot of bad code as I learned PHP, for the very reason that I did not understand the purpose of data typing.   The problem is, when a variable allows you to assign it any form of data, it adds a lot of opportunity for error.  If I have a function that I know requires an integer input, and my variable got changed to a string somewhere along the line, my function will break!

This draw back can be overcome with a little bit of proper structure and organization.  In most cases you DON’T WANT TO CHANGE TYPES!  Doing so adds to much ambiguity to your code and creates avenues for problems to occur.   It can also create a lot of headaches when your program breaks because your bool value of ‘True’ has been converted to a string value of “True”, and the comparison is not what you expect.

When using PHP and declaring variables, I still recommend staying consistent and trying to enforce your own types.  You can do this in your classes through encapsulation by enforcing input to be of a certain type inside your setter functions, prior to assigning data to your class variables.  Outside of OOP you can enforce type by creating validate functions that check type prior to assigning the data to a variable.  Either way, the extra work is worth the trouble when the amount of errors you have to respond to shrinks.

The Benefits:

The benefits of a loosely typed language are in its flexibility, especially when creating highly dynamic applications.   Purely dynamic code can be difficult to work with due to its nature.   The application is responsible for determining course of action based on conditions and data that are often unknown to the programmer.  PHP and other loosely typed languages allow this kind of ‘complex’ programming to be possible by not enforcing data types on variables.   This means you can use a variable to hold data that could be ANYTHING!

Although this could lead to very bad things if done incorrectly, it can also lead to amazing application functionality when done properly.  I have written a lot of bad code.  I have learned a lot from that bad code as well, and in result have written some applications that surprised me.  PHP allows us to set the rules by being loosely typed, which allows us far more flexibility then strictly typed languages.  But you still NEED TO SET THE RULES!

Just because PHP does not set the rules, and does not enforce that you have to, does not mean you shouldn’t.  It does however mean a little more work for you as a programmer.  I was unique in my learning of data typing, in that I learned PHP and Pearl before any strictly typed languages.   This made me unaware of the benefits of data typing for a long time.  The advantage for that is it allowed me to think outside the box right away, and now I understand the advantages and possibilities within a loosely typed language.

Wrap It Up

Now that I understand the benefits of data typing, I also can apply those concepts to my loosely typed languages, giving me the best of both worlds.  I would encourage that you do the same.  If you have not studied a language such as C# or other strict languages, you should.  Doing so will teach you the benefits of working in such a strict environment, and allow you to apply those concepts to your loosely typed language while still having all the benefits it can provide!

المزيد من الأسئلة المماثلة