Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the value of a property as the name for a method

Status
Not open for further replies.

slobad23

IS-IT--Management
Jun 30, 2006
90
GB
I am lead to believe by a book I am reading:

"It's easy to get confused between -> and => as well as when you need a $ and when you don't.

The class construction uses a dash with the greater than sign, and constructions working with associative arrays use the equal sign with the greater than sign.

The associative array used the normal form for the second variable, so it has a $ sign in front of the second variable.

There is normally no $ following $this->. You can think of the $ as meaning "use the value of what is inside." If you want to use the property $contacts, it is $this->contacts. You use the $ only if you want to use the value of what is in a property as the name of another property. The following two echo statements both display "george smith":

PHP:
$this->field = "contacts;
$this->contacts = "george smith";

echo $this->$field;
echo $this->contact

Is this just wrong? No matter what I try I can't see to get this to work in the way it is described in the book.

Here is what I currently have to demonstrate this:

PHP:
<?php 

class rar 
{	
	public $field;
	public $contact;
	
	public function printThis()
	{
		$this->field = 'contact';
		$this->contact = 'George Smith';
		echo $this->$field;
		echo $this->contact;
	}
}

$thing = new rar();

$thing->printThis();

?>

The results of my code produce nothing at all.
 
Hi

Which version of PHP ? Your code raises "Fatal error: Cannot access empty property" in PHP 5.3.12.

As far as I know, that happens because scope matters, so either :
Code:
[highlight][navy]$this[/navy][teal]->[/teal]field[/highlight] [teal]=[/teal] [green][i]'contact'[/i][/green][teal];[/teal]
[navy]$this[/navy][teal]->[/teal]contact [teal]=[/teal] [green][i]'George Smith'[/i][/green][teal];[/teal]
[b]echo[/b] [navy]$this[/navy][teal]->[/teal][highlight][teal]{[/teal][navy]$this[/navy][teal]->[/teal]field[teal]}[/teal][/highlight][teal];[/teal]
[b]echo[/b] [navy]$this[/navy][teal]->[/teal]contact[teal];[/teal]

[gray]// or[/gray]

[highlight #fcc][navy]$field[/navy][/highlight] [teal]=[/teal] [green][i]'contact'[/i][/green][teal];[/teal]
[navy]$this[/navy][teal]->[/teal]contact [teal]=[/teal] [green][i]'George Smith'[/i][/green][teal];[/teal]
[b]echo[/b] [navy]$this[/navy][teal]->[/teal][highlight #fcc][navy]$field[/navy][/highlight][teal];[/teal]
[b]echo[/b] [navy]$this[/navy][teal]->[/teal]contact[teal];[/teal]
So you have to use the same thing in both places, either a local variable or an object property.

Feherke.
[link feherke.github.com/][/url]
 
I am using php 5.4.3 // zend 2.4.0

I have found ways of working around it.

I don't actually want to achieve what I am asking here. It doesn't make much sense to me why you would want to but it's good to know the option is there.

The book I am reading ("PHP and MySQL - 24 hour trainer") mentioned this concept twice wiithin 4 pages and neither of the half written examples provide the functionality they speak about. Puts into question the whole book really - unless I am doing something silly which is why I am asking on here.

from past experience with Ruby, I decided to use the method you provided earlier:

PHP:
echo $this->{$this->field};
 
Its a little convoluted to explain, but the reason you aren't getting anything is because you have an error. When learning PHP its always a good idea to have it show any and all errors.


In your case you should be seeing this:

( ! ) Notice: Undefined variable: field in ...

That means it has no idea what $field is so can't get the property of the object who's name is supposed to be contained within it.

To clarify that particular variable could be called anything you want, but the variable itself needs to have a value.



Code:
$whatever ="contact";

echo $this->$whatever ============================== 'george smith'

Your variable could contain something else for instance that is not a property name of the object, and it also would not work. Its not the name of the variable that's important, its its contents.

Technically what the book is trying to do is the following:

Code:
[b][blue]echo[/blue][/b] [COLOR=#AA6600][b]$this[/b][/color]->[green][b]{[/b][/green][blue]$this->field[/blue][green][b]}[/b][/green];

or to more closely approach the example in the book:

Code:
$field = [COLOR=#AA6600] [b]$this[/b][/color]->[blue]field[/blue];
[b][blue]echo[/blue][/b] [COLOR=#AA6600][b]$this[/b][/color]->[COLOR=#AA6600][b]$field[/b][/color];



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
I appear to have confused myself thinking that due to dealing wtih classes that the book was referencing nothing but properties when in fact, it is dealing with a varibale inside the function/method.

What you have said makes total sense.

I did try making the variable inside the method "global" to see if that allowed it to pick up the value of the public property. No luck. It was worth a shot though.

With regards to turning on errors, I will look into that. I am running the install of apache, mysql and php as it came with Fedora.


 
The easiest way to find out where the php.ini configuration file in any PHP installation, is to run phpinfo();

Just create a new php file that has only

Code:
<?php
phpinfo();
?>

Run it, and it will show a bunch of information. One of the first few things is the location of the php.ini file it is using

Once you know that, open the file in any text editor, and set error_reporting to E-ALL and display_errors to on.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top