473,498 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strict types

Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.

PHP is a great language, but the masses of incompetant coders out there
do absolutely nothing to help the language grow. I know we all have to
start somewhere, so please don't think I'm being eliteist, because I'm
not, but something has to be done to try to fix the mess out there.

Surely the contractors on here have witnessed this, or is it just me?

What is everyone else's opinion?

Cheers,

T'rog

Mar 30 '06 #1
16 2862
On Thu, 30 Mar 2006 00:53:47 -0800, Treefrog wrote:
For a while now I've been wishing PHP had (at least the option to enable)
strict types. It would help a massive amount in BIG applications, and
maybe start to taper the millions of lines of crap code that's out there.


To be honest the only real problem is with objects. For large
applications a consistent naming scheme helps with simple variables and in
a lot of cases it's not really important what the variable is.

With regards to objects, PHP5 now has type hints which ensures a parameter
to a function or method is of a given type (class):

function bar(Foo $foo)
{
...
}

Cheers,

Andy

--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos

Mar 30 '06 #2
Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.

PHP is a great language, but the masses of incompetant coders out there
do absolutely nothing to help the language grow. I know we all have to
start somewhere, so please don't think I'm being eliteist, because I'm
not, but something has to be done to try to fix the mess out there.

Surely the contractors on here have witnessed this, or is it just me?

What is everyone else's opinion?

Cheers,

T'rog

Well, you could do it in PHP5 by wrapping all the basic types in classes
like java does for a lot of things. Then each type class would have its
own toXXX converters to handle the casts.

This is an example of 'fixed with another level of abstraction' and I
wonder at the utility of it all. PHP - like shell - is often attractive
to coders *because* it is loosely typed.

I'm not sure how many coders would find PHP as attractive if they had to
follow stricter object type coding. For example:

$a = 'This is a string';
$b = ' This is another string ';
$c = 3;
$d = $a.$b.$c;

would become something like:
$a = new String('This is a string');
$b = new String(' This is another string ');
$c = new Int(3);

$d = $a->concat($b->concat($c->toString()));

Type-safe - sure! More meaningful? I would argue that all the method
calls get in the way of the meaning of the code.

Wasn't there a phplint project to help do some high level analysis of code?

-david-

Mar 30 '06 #3
David Haynes said the following on 30/03/2006 11:43:
I'm not sure how many coders would find PHP as attractive if they had to follow stricter object type coding. For example:

$a = 'This is a string';
$b = ' This is another string ';
$c = 3;
$d = $a.$b.$c;

would become something like:
$a = new String('This is a string');
$b = new String(' This is another string ');
$c = new Int(3);

$d = $a->concat($b->concat($c->toString()));

Well, Java has strict typing, and you don't have to do this. Same in
C++ (for the most part).

--
Oli
Mar 30 '06 #4
David Haynes wrote:
Well, you could do it in PHP5 by wrapping all the basic types in classes
like java does for a lot of things. Then each type class would have its
own toXXX converters to handle the casts.
Cute. I might spend some time playing with that idea. but I'd prefer if
PHP had built in support...

This is an example of 'fixed with another level of abstraction' and I
wonder at the utility of it all. PHP - like shell - is often attractive
to coders *because* it is loosely typed.

I'm not sure how many coders would find PHP as attractive if they had to
follow stricter object type coding.


Hmm, I agree I think, hence wanting the option to use strict types. The
problem is, from my experience, people initially use PHP because it's
easy and they can get their idea realised in a short time with a
minimal leanring curve/expense. Unfortunately, soem of these ideas
actually work and start making money, sometimes BIG money, and they are
left with poorley coded legacy crap that everybody firefights to keep
working... and any improvments are complete kluges.

I'm not sure what can be done to change that, but I want to stay with
PHP, it can be used for huge scalable systems, but only if coded
properly. I suppose I'm clutching at straws to make it harder to write
bad code.

Mar 30 '06 #5
Oli Filth wrote:
David Haynes said the following on 30/03/2006 11:43:
> I'm not sure how many coders would find PHP as attractive if they had to
follow stricter object type coding. For example:

$a = 'This is a string';
$b = ' This is another string ';
$c = 3;
$d = $a.$b.$c;

would become something like:
$a = new String('This is a string');
$b = new String(' This is another string ');
$c = new Int(3);

$d = $a->concat($b->concat($c->toString()));

Well, Java has strict typing, and you don't have to do this. Same in
C++ (for the most part).

Yes, they do, but they are designed into the language from the
beginning. I was operating from the assumption of using what was
available today in PHP5.

-david-

Mar 30 '06 #6
Treefrog wrote:
David Haynes wrote:
Well, you could do it in PHP5 by wrapping all the basic types in classes
like java does for a lot of things. Then each type class would have its
own toXXX converters to handle the casts.


Cute. I might spend some time playing with that idea. but I'd prefer if
PHP had built in support...

This is an example of 'fixed with another level of abstraction' and I
wonder at the utility of it all. PHP - like shell - is often attractive
to coders *because* it is loosely typed.

I'm not sure how many coders would find PHP as attractive if they had to
follow stricter object type coding.


Hmm, I agree I think, hence wanting the option to use strict types. The
problem is, from my experience, people initially use PHP because it's
easy and they can get their idea realised in a short time with a
minimal leanring curve/expense. Unfortunately, soem of these ideas
actually work and start making money, sometimes BIG money, and they are
left with poorley coded legacy crap that everybody firefights to keep
working... and any improvments are complete kluges.

I'm not sure what can be done to change that, but I want to stay with
PHP, it can be used for huge scalable systems, but only if coded
properly. I suppose I'm clutching at straws to make it harder to write
bad code.


I think it ends up being more organic. You start to develop a set of
libraries and practices that protect you from a lot of coding goofs and
continue to build/refine.

One of the more humbling things I think any good coder can do is look at
their code from a year or two before. I know I usually end up wondering
what I was thinking in some places ;-)

With the stronger object support in PHP5, a lot of things can be coded
to be protected well and, as with all programming languages, it is
possible to write good and bad code regardless of the protection the
language offers.

One other way people have handled the 'type' issue is to use a strong
naming convention - e.g. Hungarian notation. Personally, I can't stand
using it but others find it helpful.

-david-

Mar 30 '06 #7
Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.

PHP is a great language, but the masses of incompetant coders out there
do absolutely nothing to help the language grow. I know we all have to
start somewhere, so please don't think I'm being eliteist, because I'm
not, but something has to be done to try to fix the mess out there.

Surely the contractors on here have witnessed this, or is it just me?

What is everyone else's opinion?

Cheers,

T'rog


Hi, T'rog,

Well, one of the big attractions to PHP is the lack of strict typing. You don't
have to go through all the overhead of declaring variable types (or even
variables unless they are class members). Getting rid of the "administrative
overhead" allows you to concentrate on the problem.

You can write good code in any language (except maybe COBOL :-) ). You can
write bad code in any language - including Java and C++. However, IMHO, strict
typing does in general get you to write "better" code.

I think a good compromise between old and new would be a "strict" flag. It
could be set in the php.ini file or .htaccess (on Apache, if allowed). Perhaps
it could even be a parse-time command (ini_set isn't executed until too late),
similar to C/C++'s pragma statement. The last would allow someone to convert a
website one file at a time to strict. Then when everything's changed, change
the php.ini file, or, if on a shared server, at a statement to .htaccess.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 30 '06 #8
Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.


Generally speaking, PHP is a scripting language. Scripting languages are
great for one-off projects and small programs, but become unwieldy in
big applications, larger than a few thousand lines. For larger projects,
I would suggest Java, which has strict typing and encourages
object-oriented programming and modularization.
Mar 30 '06 #9
Steve Chapel wrote:
Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.


Generally speaking, PHP is a scripting language. Scripting languages are
great for one-off projects and small programs, but become unwieldy in
big applications, larger than a few thousand lines. For larger projects,
I would suggest Java, which has strict typing and encourages
object-oriented programming and modularization.


No no, I can already code c# (so probably Java too), that has strict
types and great oo support, but I've grown up using PHP, all my
professional career has been mainly PHP based and I want to stay with
the language. I've seen it evolve to inlcude basic OO support, then
more advanced OO....

Once upon a time, PHP was a scriting language, but it's more than that
now. For example, I'm currently working with a 500,000 line 100% PHP
application. Because 95% of it is built on poor coding, it's nearly
impossible, certainly futile to try to impliment standards into it. I
was just trying to think of/ask for ways in which situations like this
can be stopped in the future. Which, I think needs to happen for PHP's
future. I've worked in quite a few places, and with quite a few other
developers, and they seem to agree; PHP code is nearly always messy
crap.

But it doesn't have to be.

Mar 30 '06 #10
Treefrog wrote:
Once upon a time, PHP was a scriting language, but it's more than that
now. For example, I'm currently working with a 500,000 line 100% PHP
application. Because 95% of it is built on poor coding, it's nearly
impossible, certainly futile to try to impliment standards into it. I
was just trying to think of/ask for ways in which situations like this
can be stopped in the future. Which, I think needs to happen for PHP's
future. I've worked in quite a few places, and with quite a few other
developers, and they seem to agree; PHP code is nearly always messy
crap.

But it doesn't have to be.


I'm not so sure about that. Large Perl programs (and even many small
programs) are nearly always messy crap. I believe it's a natural
property of languages that have weak or loose typing, do not require you
to declare variables, and do not have a separate compilation step. These
kinds of languages are often called scripting languages
<http://en.wikipedia.org/wiki/Scripting_languages>.

Perhaps there is a way to make scripting languages more suited for large
applications. Discussion of how to do that may be beyond the scope of a
post in a PHP newsgroup, however.
Mar 30 '06 #11

Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.


Eliminating badcode by altering the language--that sounds rather
Orwellian to me.

Mar 31 '06 #12
Chung Leong wrote:
Treefrog wrote:
Hi All,

For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.


Eliminating badcode by altering the language--that sounds rather
Orwellian to me.

You mean double plus ungood?
Mar 31 '06 #13
Chung Leong wrote:

Eliminating badcode by altering the language--that sounds rather
Orwellian to me.


You're absolutely right. My most humble apologese. Let the bad code
prevail!

Mar 31 '06 #14

Treefrog wrote:
Chung Leong wrote:

Eliminating badcode by altering the language--that sounds rather
Orwellian to me.


You're absolutely right. My most humble apologese. Let the bad code
prevail!


Just because people can make illogical statements in English does not
imply that stupidity will prevail.

Mar 31 '06 #15

Chung Leong wrote:
Just because people can make illogical statements in English does not
imply that stupidity will prevail.


Au contraire, mon ami!

Mar 31 '06 #16
"Treefrog" <in**@designstein.co.uk> wrote:
For a while now I've been wishing PHP had (at least the option to
enable) strict types. It would help a massive amount in BIG
applications, and maybe start to taper the millions of lines of crap
code that's out there.


Starting from the same thought, I realized that a scripting language has
the nice property to allows for fast code development of little programs,
like most the simpler WEB pages indeed are. That's way PHP is so popular.

Faced with the perspective to switch to another strong-typed language
in order to develop some complex applications, I discovered another
development pattern, that is: write your messy code, then check it with
a formal validator.

Problem: no PHP formal validators seem to be available. That's way
I wrote PHPLint, a formal validator for PHP4 and PHP5. It takes your
sources and spots errors and potential problems, like:

unused constants/variables/functions/classes/properties/methods (for
short: "items");

type mismatch in items usage (you can't add a string to a number without
an explicit typecast);

type mismatch in function arguments (you can't pass a string to a function
requiring an array, for example);

all the properties of a class must be declared; their type given by the
initial value or specified through PHPLint meta-code.

This sample of code should give an idea. The comments summaryze the complains
of the validator:

<?php

class Example {
private $aString = "something"; # inferred type: string
public /*. int .*/ $anInt;
public $xxx = 0;

public function __construct(/*. string .*/ $s, $i=0)
{ $this->aString = $s; $this->anInt = $i; }
# inferred method signature: void(string [, int])

public function getString()
{ return $this->aString; }
# inferred method signature: string()
}

$obj = new Example("hello");
# inferred var. type: class Example

$obj->aString = ""; # <-- ERROR: accessing private property

$obj->gggg = 999; # <-- ERROR: no property gggg in class Example

$obj2 = new Example(123); # <-- ERROR: expected arg of type string

$num = 3;
# inferred var. type: int

$num = $obj; # <-- ERROR: type mismatch in assignment
$num += (int) $obj->getString();
$num += $obj->getString(); # <-- ERROR: type mismatch

if( $num === 0 ) { $num = 1; } # ok
if( $num ) { } # <-- ERROR: invalid bool expr

$fp = fopen("data1.txt", "r");
if( $fp === FALSE )
die("can't open data.txt");

$fp = fopen("data2.txt", "r") || die("xxx"); # ERROR: invalid expr

?>

# notice: the property `anInt' used only inside its class, you should make it `private'
# unused property `Example::$xxx'
?>

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Apr 30 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
296
by: droope | last post by:
I have a routine that does a standard comparison that I pass two objects to Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolea ' Compares two values to determine if...
2
2448
by: Bryan Parkoff | last post by:
I create one 32 Bits variable and four pointer variables. Four pointer variables link or point to one 32 Bits variable. Each pointer variable is 8 Bits. Look at my example below. unsigned int...
20
3724
by: nicolas.riesch | last post by:
I try to understand strict aliasing rules that are in the C Standard. As gcc applies these rules by default, I just want to be sure to understand fully this issue. For questions (1), (2) and...
2
12335
by: Sam Sungshik Kong | last post by:
Hello! I studied C# a little bit and am trying to compare it with VB.Net. There's 'Option Strict' in VB.Net. I thought that if I turn it on, it is as strict as C# when checking types. See...
12
1515
by: Simon Harris | last post by:
Hi All, I've been advised to use option strict. I've tried to read up on this, all i can find is that it... "disallows implicit narrowing conversions" This kinda makes sense - Means I have...
3
1670
by: Jorge Cavalheiro | last post by:
Hello After reading yesterday's thread on performance of C# and vb.net i've just turn it on my solution and spent 2 hours correcting the errors :) I have a problem i don't how to fix i have a...
2
1317
by: Justin | last post by:
I have set Option Strict On I also have Enum AssemblyLineStatus As Integer My question is how come there is no compilation error when I compile following code? If...
13
2303
by: C. Moya | last post by:
I fully expected the lack of a way to set Option Strict globally to be fixed in SP1. I can't seem to figure out if it has been fixed or not. It still seems we have to add the declaration at the top...
18
2855
by: Poldie | last post by:
How do I turn it on? I'm using vb 2005 in visual studio 2005 sp1. In my web.config I have: <compilation debug="true" strict="true" /> In my Tools/Options/Projects and solutions/vb defaults...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.