Forum Moderators: coopster
$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.
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
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.
<?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"
?>
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;
$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 ;)
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... : )
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]