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

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

P: n/a
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.txt

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_field = &New "cls_validate_"$field_name;
14 $var_strip_field = &New "cls_strip_"$field_name;
15 $record[$field_name] = $_POST[$field_name];
16 $record_check[$field_name] =
$var_validate_field->fn_validate($record[$field_name]);
17 $record[$field_name] =
$var_strip_field->fn_strip($record[$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.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
N. David wrote:
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.


foreach ($field as $field_name) {
$cls_validate = "cls_validate_" . $field_name;
$var_validate_field = &New $cls_validate;
...
}

Please read:

http://www.php.net/manual/en/languag...ors.string.php
JW

Jul 17 '05 #2

P: n/a
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

http://www.tonymarston.net

"N. David" <nd*****@yahoo.com> wrote in message
news:6f**************************@posting.google.c om...
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.txt

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_field = &New "cls_validate_"$field_name;
14 $var_strip_field = &New "cls_strip_"$field_name;
15 $record[$field_name] = $_POST[$field_name];
16 $record_check[$field_name] =
$var_validate_field->fn_validate($record[$field_name]);
17 $record[$field_name] =
$var_strip_field->fn_strip($record[$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.

Jul 17 '05 #3

P: n/a
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message news:<ce*******************@news.demon.co.uk>...
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.


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.
Jul 17 '05 #4

P: n/a

"N. David" <nd*****@yahoo.com> wrote in message
news:6f**************************@posting.google.c om...
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.txt

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_field = &New "cls_validate_"$field_name;
14 $var_strip_field = &New "cls_strip_"$field_name;
15 $record[$field_name] = $_POST[$field_name];
16 $record_check[$field_name] =
$var_validate_field->fn_validate($record[$field_name]);
17 $record[$field_name] =
$var_strip_field->fn_strip($record[$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.


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;

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.