473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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.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
4 5312
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
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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
10
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having...
1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
6
by: Jordi | last post by:
I'm having a problem which I think has to do with the circular use of inlcuding header files. The short version of my code would look a bit like this, I think: GameEngine.h: #ifndef GAME_ENGINE...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
2
by: joe t. | last post by:
My apologies, i'm not sure how to ask this question correctly, so i'll give the examples as i go. First, i *think* my problem is described here, but i may be misunderstanding:...
6
by: j.woodcock | last post by:
is there a way of having a file that's name is a variable (eg dependant on the user name) act like a include. i know that you cant define the file for an include asp tag using a variable and that...
1
by: arggg | last post by:
I have an external html that is a form. the Values of the form elements have <?= $results ?> type information however with file_get_contents in the main php file that then stores the contents to a...
109
by: bonneylake | last post by:
Hey Everyone, Well i am having a problem outputting for my report and hoping someone can explain what i am doing wrong. What the report is about is comparing my company's part price's to d and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
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,...

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.