How do you include a variable as part of the name of a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • N. David

    How do you include a variable as part of the name of a class

    In bash, you can include a variable as part of a filename. For
    example:

    date=$(date)
    cat log.txt > $date"_log.txt"

    Which, when setting the right options in date, creates a file named
    20040705_log.tx t

    I am now trying to call a class based on a previously defined variable
    set by an array:

    1 $field[1] = "first_name ";
    7 $field[2] = "mi";
    8 $field[3] = "last_name" ;
    12 foreach ($field as $field_name) {
    13 $var_validate_f ield = &New "cls_validate_" $field_name;
    14 $var_strip_fiel d = &New "cls_strip_"$fi eld_name;
    15 $record[$field_name] = $_POST[$field_name];
    16 $record_check[$field_name] =
    $var_validate_f ield->fn_validate($r ecord[$field_name]);
    17 $record[$field_name] =
    $var_strip_fiel d->fn_strip($reco rd[$field_name]);
    18 print "<p>$field_ name is $record[$field_name] and its check is
    $record_check[$field_name]";
    19 }

    As you can see, I am trying to make $field_name part of the name of
    the class, but it is not working. Everything else works fine; I've
    tested the classes individually; but the array method will not work
    without this. Suggestions are welcome. Thank you.
  • Janwillem Borleffs

    #2
    Re: How do you include a variable as part of the name of a class

    N. David wrote:[color=blue]
    > As you can see, I am trying to make $field_name part of the name of
    > the class, but it is not working. Everything else works fine; I've
    > tested the classes individually; but the array method will not work
    > without this. Suggestions are welcome. Thank you.[/color]

    foreach ($field as $field_name) {
    $cls_validate = "cls_valida te_" . $field_name;
    $var_validate_f ield = &New $cls_validate;
    ...
    }

    Please read:




    JW



    Comment

    • Tony Marston

      #3
      Re: How do you include a variable as part of the name of a class

      Your code is terribly inefficient. Instead of iterating through the array
      and calling a different method to validate each field individually why don't
      you pass the whole array to a single validation method which can iterate
      through the array internally. As the $_POST array is an associative array of
      name=value pairs then the validation method can easily identify which fields
      have been passed to it for validation.

      This is explained in more detail in my articles at
      http://www.tonymarston.co.uk/php-mys...seobjects.html and
      http://www.tonymarston.co.uk/php-mys...eobjects2.html.

      --
      Tony Marston

      This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




      "N. David" <ndavidg@yahoo. com> wrote in message
      news:6fa7cbb4.0 408051056.11aa6 091@posting.goo gle.com...[color=blue]
      > In bash, you can include a variable as part of a filename. For
      > example:
      >
      > date=$(date)
      > cat log.txt > $date"_log.txt"
      >
      > Which, when setting the right options in date, creates a file named
      > 20040705_log.tx t
      >
      > I am now trying to call a class based on a previously defined variable
      > set by an array:
      >
      > 1 $field[1] = "first_name ";
      > 7 $field[2] = "mi";
      > 8 $field[3] = "last_name" ;
      > 12 foreach ($field as $field_name) {
      > 13 $var_validate_f ield = &New "cls_validate_" $field_name;
      > 14 $var_strip_fiel d = &New "cls_strip_"$fi eld_name;
      > 15 $record[$field_name] = $_POST[$field_name];
      > 16 $record_check[$field_name] =
      > $var_validate_f ield->fn_validate($r ecord[$field_name]);
      > 17 $record[$field_name] =
      > $var_strip_fiel d->fn_strip($reco rd[$field_name]);
      > 18 print "<p>$field_ name is $record[$field_name] and its check is
      > $record_check[$field_name]";
      > 19 }
      >
      > As you can see, I am trying to make $field_name part of the name of
      > the class, but it is not working. Everything else works fine; I've
      > tested the classes individually; but the array method will not work
      > without this. Suggestions are welcome. Thank you.[/color]


      Comment

      • N. David

        #4
        Re: How do you include a variable as part of the name of a class

        "Tony Marston" <tony@NOSPAM.de mon.co.uk> wrote in message news:<ceud2c$kp q$1$830fa79f@ne ws.demon.co.uk> ...[color=blue]
        > Your code is terribly inefficient. Instead of iterating through the array
        > and calling a different method to validate each field individually why don't
        > you pass the whole array to a single validation method which can iterate
        > through the array internally. As the $_POST array is an associative array of
        > name=value pairs then the validation method can easily identify which fields
        > have been passed to it for validation.
        >
        > This is explained in more detail in my articles at
        > http://www.tonymarston.co.uk/php-mys...seobjects.html and
        > http://www.tonymarston.co.uk/php-mys...eobjects2.html.
        >[/color]

        So what you are telling me is that the "for each" is unecessary and I
        can just pass the whole array one time to the class file? I will try
        this, thanks.

        Comment

        • Chung Leong

          #5
          Re: How do you include a variable as part of the name of a class


          "N. David" <ndavidg@yahoo. com> wrote in message
          news:6fa7cbb4.0 408051056.11aa6 091@posting.goo gle.com...[color=blue]
          > In bash, you can include a variable as part of a filename. For
          > example:
          >
          > date=$(date)
          > cat log.txt > $date"_log.txt"
          >
          > Which, when setting the right options in date, creates a file named
          > 20040705_log.tx t
          >
          > I am now trying to call a class based on a previously defined variable
          > set by an array:
          >
          > 1 $field[1] = "first_name ";
          > 7 $field[2] = "mi";
          > 8 $field[3] = "last_name" ;
          > 12 foreach ($field as $field_name) {
          > 13 $var_validate_f ield = &New "cls_validate_" $field_name;
          > 14 $var_strip_fiel d = &New "cls_strip_"$fi eld_name;
          > 15 $record[$field_name] = $_POST[$field_name];
          > 16 $record_check[$field_name] =
          > $var_validate_f ield->fn_validate($r ecord[$field_name]);
          > 17 $record[$field_name] =
          > $var_strip_fiel d->fn_strip($reco rd[$field_name]);
          > 18 print "<p>$field_ name is $record[$field_name] and its check is
          > $record_check[$field_name]";
          > 19 }
          >
          > As you can see, I am trying to make $field_name part of the name of
          > the class, but it is not working. Everything else works fine; I've
          > tested the classes individually; but the array method will not work
          > without this. Suggestions are welcome. Thank you.[/color]

          An inconsistency in PHP. Variable class name works with variables but not
          literal strings or evaluated values. Do this:

          $class = "cls_validate_$ field_name";
          $obj = new $class;



          Comment

          Working...