By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

How to check if a typecast succeeds?

P: n/a
I have a problem with typecast methods.

Here is a setter of an object:
function setKind($kind)
{
if (is_int((int)$kind) and (strlen($kind)<=6)) {
$this->kind = $kind;
return true;
}
else {
return false;
}
}
My goal is, not to allow to set float or string values or too big
integer values (max. 6 digits!) for an object property.

I need to check, if the $kind is an int, but the function argument $kind
is an string from the database or an input field.
I don't know how to convert and decline illegal aruments.

If the typecast fails in my upper sample, the function returns TRUE
though. That is not correct! It has to return FALSE.

I thought about is_numeric, but that would accept float values as well
and are not allowed to set (ONLY int).

does any body know how to handle that problem?
thanx in advance,
Lars

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Lars Plessmann wrote:
I have a problem with typecast methods.

Here is a setter of an object:
function setKind($kind)
{
if (is_int((int)$kind) and (strlen($kind)<=6)) { .... If the typecast fails in my upper sample, the function returns TRUE
though. That is not correct! It has to return FALSE.

is_int((int)$whatever) is *always* true!

You're typecasting $whatever to an int and PHP will do just that no
matter what is in the variable (in the worst case it will convert to 0).

$x = 'Lars';
$y = (int)$x; // $y = 0;

does any body know how to handle that problem?

Maybe like this?

function setKind($kind) {
if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
return $retval;
}

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2

P: n/a
Pedro Graca wrote:
Lars Plessmann wrote:
I have a problem with typecast methods.

Here is a setter of an object:
function setKind($kind)
{
if (is_int((int)$kind) and (strlen($kind)<=6)) {


...
If the typecast fails in my upper sample, the function returns TRUE
though. That is not correct! It has to return FALSE.


is_int((int)$whatever) is *always* true!

You're typecasting $whatever to an int and PHP will do just that no
matter what is in the variable (in the worst case it will convert to 0).

$x = 'Lars';
$y = (int)$x; // $y = 0;
does any body know how to handle that problem?


Maybe like this?

function setKind($kind) {
if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
return $retval;
}


THanks for the answer, Pedro. Now I know why it didn't work. Thats not
good that it returns only TRUE :-(

I need to give feedback to the point, where function was called: "it
worked" (true) OR "not it didn't work" (false)

Is there no other possibility to do it?
Jul 17 '05 #3

P: n/a
Lars Plessmann wrote:
Pedro Graca wrote:
function setKind($kind) {
if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
return $retval;
}
I need to give feedback to the point, where function was called: "it
worked" (true) OR "not it didn't work" (false)

Is there no other possibility to do it?


Have you tried replacing your setKind() function with the one I posted
earlier?

I tend to make short code ... that function is the exact same thing as:

function setKind($kind) {
// check whether $kind is composed of 1 to 6 digits
// it it is put TRUE in $retval; if it isn't put FALSE there
// save $retval to use later, in the return statement
$retval = preg_match('/^\d{1,6}$/', $kind);
// the regexp means:
// ^ -- start of subject
// \d -- digit
// {1,6} -- repeatition of last 'thing' (the digit) 1 to 6 times
// $ -- end of subject

if ($retval == true) {
// only sets $this->kind if the parameter was 1 to 6 digits
// but make it an int
$this->kind = (int)$kind;
}

// return the result of checking if the parameter was 1 to 6 digits
return $retval;
}

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4

P: n/a
Pedro Graca wrote:
Lars Plessmann wrote:
Pedro Graca wrote:
function setKind($kind) {
if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
return $retval;
}


I need to give feedback to the point, where function was called: "it
worked" (true) OR "not it didn't work" (false)

Is there no other possibility to do it?

Have you tried replacing your setKind() function with the one I posted
earlier?

I tend to make short code ... that function is the exact same thing as:

function setKind($kind) {
// check whether $kind is composed of 1 to 6 digits
// it it is put TRUE in $retval; if it isn't put FALSE there
// save $retval to use later, in the return statement
$retval = preg_match('/^\d{1,6}$/', $kind);
// the regexp means:
// ^ -- start of subject
// \d -- digit
// {1,6} -- repeatition of last 'thing' (the digit) 1 to 6 times
// $ -- end of subject

if ($retval == true) {
// only sets $this->kind if the parameter was 1 to 6 digits
// but make it an int
$this->kind = (int)$kind;
}

// return the result of checking if the parameter was 1 to 6 digits
return $retval;
}


great, yeah it works.
but I don't know how regular expressions work.

I need also to check for a
TIMESTAMP (YYYYMMDDHHmmSS), DATE (DD.MM.YYYY) and FLOAT (123456789.33)
value.
Do you know the patterns for these elements?
I really thank you, Pedro
Lars
Jul 17 '05 #5

P: n/a
Lars Plessmann wrote:
but I don't know how regular expressions work.
They're hard. They're worth it! :-)

I need also to check for a
TIMESTAMP (YYYYMMDDHHmmSS),
preg_match('/^\d{14}$/', $variable)

but this will match any sequence of 14 digits, for
instance, 20872718529000.

I think doing a preg_match() to validate TIMESTAMPS isn't the best way
to do it.

DATE (DD.MM.YYYY)
preg_match('/^[0123][0-9]\.[01][0-9]\.[1-2]\d{3}$/', $variable)

and then you should isolate the date components and check for date
validity, perhaps with checkdate(), so that 30 February does not go
through ... but if you're going to do that, why do the preg_match() in
the first place?
Again, doing a preg_match() is not the best option here.

and FLOAT (123456789.33) value.


preg_match('/^\d{1,}(\.\d{0,})?$/', $variable)

{1,} means 1 or more and can be replaced with +
{0,} means 0 or more and can be replaced with *

same as above
preg_match('/^\d+(\.\d*)?$/', $variable)

[nitpickers]
The last regexp is capturing the fractional part.
To avoid that I'd do

preg_match('/^\d+(?:\.\d*)$/', $variable)
[/nitpickers]

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.