Forum Moderators: coopster

Message Too Old, No Replies

How can I put the name of a variable into a string?

         

pixeltierra

6:49 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



Say I have the object $photography, which also has a property $name. I want to dynamically set the variable $photography->name="photography", when the obj is instantiated. Is this possible?

Psychopsia

8:14 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



Hi!
Yes, it's possible. Set the var in the constructor.

pixeltierra

9:11 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



I might not have explained myself well. This is what I want to do.

$photography = new category();
echo $photography->name; //writes "photography"

I want to dynamically set the $name var to be a string representation of the instance name. Does that make sense?

Psychopsia

9:32 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



So, do you want to print the class name or the value in class var $name?

If I understand you:

class photography {
var $name;

function photography() // Constructor
{
$this->name = 'some string';
}
}

$photography = new photography();
echo $photography->name; // Prints 'some string'

Is this correct?

pixeltierra

9:57 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



No, that's not what I want. Look at this code:

$photography = new category();
echo $photography->name; //writes "photography"

$design= new category();
echo $design->name; //writes "design"

$whatever = new category();
echo $whatever->name; //writes "whatever"

The class is category{}, not photography. $photography is an instance of the category class. $design is a separate instance of the category class. The category class has a var $name. What I want, as written above, is as soon as the class category is instantiated (i.e. $photography = new category(), or $design = new category(), or whatever) I want a variable $instance->name availabe whose value is the text of the variable name.

The code above explains this better than I have.

mcibor

10:56 pm on Nov 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope, you can't do that automatically, because when you assign $ph = new category(), then you assign a category to variable $ph. The class category doesn't get any information about the variable. However what you can do is:

in class declaration:

class category {
var $name;
...
}

on class creation:

$photography = new category();
$photography -> name = 'photography';
//then
echo $photography -> name; //writes "photography"

Hope this helps you to solve your problem - I don't think, that there is any way to put it into class declaration
Regards
Michal

pixeltierra

11:44 pm on Nov 12, 2006 (gmt 0)

10+ Year Member



Thanks for the input Mcibor.

What you recommend is what I'm doing now, manually declaring $instance->name = "x", but I wanted to reduce the amount of "configuring" the object before using it, and it just seems there should be a way to do this.

You can go the other way and make a variable out of a string, like this $$name, but so far, it seems you can't make a string out of a variable.

Stuperfied

12:22 am on Nov 13, 2006 (gmt 0)

10+ Year Member



Sorry for the late reply, took a while to work it up. Its a bit more work than just assigning it manually but hope this helps anyway.

<?php
class category {
var $name;
}
// then
function getLast() {
$test="hello world";
end($GLOBALS);
$last=key($GLOBALS);
return $last;
}
// then
$photography = new category();
$photography -> name = getLast();
// then
echo $photography -> name; //writes "photography"
?>

pixeltierra

3:08 am on Nov 13, 2006 (gmt 0)

10+ Year Member



Stuperfied: thanks for your help and energy I appreciate it.

That is brilliant, and nearly almost what I need. The use of $GLOBALS is great and the leap I couldn't make myself.

Unfortunately, adding the line $photography->name = $photography->getLast() is similar to $photography->name = "photography, in that it takes that extra step to set up the object.

To give you give you an idea of what I'm trying to do, I'm writing a form generation class. On one form I can have up to 30 fields, and every one is set up similarly (below). I just am trying to avoid the step of setting the name, since it is already done in the name of the object.

Your solution (which /IS/ brilliant) nearly worked this way:

//CONSTRUCTOR
function textArea (){
end($GLOBALS);
$this->name = key($GLOBALS);
return key($GLOBALS);
}

But as it turns out the object isn't instantiated until the constructor is finished (and all if its calls), so $this->name is always the name of the LAST object created. Oh well. I give up for now.

$name_1 = new textField();
$name_1->name = 'name_1';
$name_1->label = 'First Name';
$name_1->pad_label = false;
$name_1->max = 30;
$name_1->php_validator = 'no_empty';

$name_2 = new textField();
$name_2->name = 'name_2';
$name_2->label = 'Last Name';
$name_2->pad_label = false;
$name_2->max = 30;
$name_2->php_validator = 'no_empty';

$email = new textField();
$email->name = 'email';
$email->label = 'Email';
$email->pad_label = false;
$email->max = 30;
$email->php_validator = 'validate_email';

$phone = new textField();
$phone->name = 'phone';
$phone->label = 'Phone';
$phone->pad_label = false;
$phone->max = 12;
$phone->php_validator = 'validate_phone';

$need = new radioSet();
$need->name = 'need';
$need->php_validator = 'no_empty';
$need->data=array("new" => "New site",
"remodeling" => "Remodeling of existing site",
"project" => "Specific Project on existing site"
);
$existing_site = new textField();
$existing_site->name = 'existing_site';
$existing_site->label = 'Existing Site Domain';
$existing_site->pad_label = false;
$existing_site->max = 30;

$purpose = new textArea();
$purpose->name = 'purpose';
$purpose->label = 'Briefly describe the purpose of your web site:';
$purpose->pad_label = false;
$purpose->php_validator = 'no_empty';

$urgency = new selectField();
$urgency->name = 'urgency';
$urgency->label = 'How urgent is this project:';
$urgency->php_validator = 'no_empty';
$urgency->data=array("1" => "very urgent",
"2" => "somewhat urgent",
"3" => "no hurry",
"4" => "sometime this year"
);
$visitors_do = new textArea();
$visitors_do->name = 'visitors_do';
$visitors_do->label = 'What do you want visitors to your site to be able to do?';
$visitors_do->pad_label = false;
$visitors_do->php_validator = 'no_empty';

$pages = new selectField();
$pages->name = 'pages';
$pages->label = 'Approximately how many pages will the website contain:';
$pages->php_validator = 'no_empty';
$pages->data=array("1" => "1-5",
"2" => "5-20",
"3" => "20-50",
"4" => "50-200",
"5" => "500+"
);
$architecture = new selectField();
$architecture->name = 'architecture';
$architecture->label = 'What type of information architecture (menu/navigation structure) best applies to your site?';
$architecture->php_validator = 'no_empty';
$architecture->data=array("1" => "Simple list of single pages (under 5)",
"2" => "Several main sections with multiple pages in each section",
"3" => "Main sections with many pages in each section",
"4" => "Sections, with subsections, with subsections…",
"5" => "Too complex to describe in writing. "
);
$sales_online = new radioSet();
$sales_online->name = 'sales_online';
$sales_online->label = 'Do you want to make sales online?';
$sales_online->php_validator = 'no_empty';
$sales_online->data =array("yes" => "yes",
"no" => "no",
);

$database = new textArea();
$database->name = 'database';
$database->label = 'Will you need a database? For what purpos(es):';
$database->pad_label = false;
$database->rows = 3;

eelixduppy

3:18 am on Nov 13, 2006 (gmt 0)



For simplicity, something like this should work, although I haven't tested it out:

$test = "this is a test of your home alarm receivers";
$vars = get_defined_vars();
echo array_search($test,$vars);

....I could just be going crazy ;)

pixeltierra

5:42 am on Nov 13, 2006 (gmt 0)

10+ Year Member



I hope I'm not beating a dead horse by now : )

eelixduppy: your method could help maybe if if get_defined_vars weren't scoped.

I need something I can call in a constructor that get's the name of the object just constructed into a string. Except that the object isn't actually instantiated until the constructor is finished all calls. So I think I'm SOL on this one : (

All genius welcome to participate in this thread... : )

Stuperfied

4:34 am on Nov 14, 2006 (gmt 0)

10+ Year Member



As a part of good coding practice, you should really be registering your names before you use them anyway. I dont allways do it myself either, as you might have noticed. If you do so as you go and then add the code I provided to your constructor, then you should have no problems.

Hope this helps...


<?php
class category {
var $name;
function category() {
end($GLOBALS);
$this->name=key($GLOBALS);
return $this->name;
}
}
// then
$photography=""; // Define first to add it to global name space.
$photography = new category();
// then
echo $photography -> name; //writes "photography"
// then continue
$check="";
$check = new category();
echo " " . $check -> name; //writes "check"
?>

[edited by: Stuperfied at 4:53 am (utc) on Nov. 14, 2006]