How to check if a typecast succeeds?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars Plessmann

    #1

    How to check if a typecast succeeds?

    I have a problem with typecast methods.

    Here is a setter of an object:


    function setKind($kind)
    {
    if (is_int((int)$k ind) 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

  • Pedro Graca

    #2
    Re: How to check if a typecast succeeds?

    Lars Plessmann wrote:[color=blue]
    > I have a problem with typecast methods.
    >
    > Here is a setter of an object:
    >
    >
    > function setKind($kind)
    > {
    > if (is_int((int)$k ind) and (strlen($kind)< =6)) {[/color]
    ....[color=blue]
    > If the typecast fails in my upper sample, the function returns TRUE
    > though. That is not correct! It has to return FALSE.[/color]


    is_int((int)$wh atever) 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;

    [color=blue]
    > does any body know how to handle that problem?[/color]


    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 :

    Comment

    • Lars Plessmann

      #3
      Re: How to check if a typecast succeeds?

      Pedro Graca wrote:
      [color=blue]
      > Lars Plessmann wrote:
      >[color=green]
      >>I have a problem with typecast methods.
      >>
      >>Here is a setter of an object:
      >>
      >>
      >> function setKind($kind)
      >> {
      >> if (is_int((int)$k ind) and (strlen($kind)< =6)) {[/color]
      >
      > ...
      >[color=green]
      >>If the typecast fails in my upper sample, the function returns TRUE
      >>though. That is not correct! It has to return FALSE.[/color]
      >
      >
      >
      > is_int((int)$wh atever) 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;
      >
      >
      >[color=green]
      >>does any body know how to handle that problem?[/color]
      >
      >
      >
      > Maybe like this?
      >
      > function setKind($kind) {
      > if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
      > return $retval;
      > }
      >[/color]

      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?

      Comment

      • Pedro Graca

        #4
        Re: How to check if a typecast succeeds?

        Lars Plessmann wrote:[color=blue]
        > Pedro Graca wrote:[color=green]
        >> function setKind($kind) {
        >> if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
        >> return $retval;
        >> }[/color][/color]
        [color=blue]
        > 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?[/color]

        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 :

        Comment

        • Lars Plessmann

          #5
          Re: How to check if a typecast succeeds?

          Pedro Graca wrote:
          [color=blue]
          > Lars Plessmann wrote:
          >[color=green]
          >>Pedro Graca wrote:
          >>[color=darkred]
          >>>function setKind($kind) {
          >>> if ($retval = preg_match('/^\d{1,6}$/', $kind)) $this->kind = (int)$kind;
          >>> return $retval;
          >>>}[/color][/color]
          >
          >[color=green]
          >>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?[/color]
          >
          >
          > 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;
          > }
          >[/color]

          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

          Comment

          • Pedro Graca

            #6
            Re: How to check if a typecast succeeds?

            Lars Plessmann wrote:[color=blue]
            > but I don't know how regular expressions work.[/color]

            They're hard. They're worth it! :-)

            [color=blue]
            > I need also to check for a
            > TIMESTAMP (YYYYMMDDHHmmSS ),[/color]

            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.

            [color=blue]
            > DATE (DD.MM.YYYY)[/color]

            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.

            [color=blue]
            > and FLOAT (123456789.33) value.[/color]

            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 :

            Comment

            Working...