473,326 Members | 2,126 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,326 software developers and data experts.

Why can't I reference a member variable?

Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to OOP
in general. I've spent the last three hours trying to figure out why this
code snipped doesn't do what I expect it to:

---snip---
<?php

$myTest = new Test;

echo $myTest->GetClassName; // Nada

class Test
{

// Class constructor
function Test()
{
// Empty on purpose...
}

// Returns class name
function GetClassName()
{
return "Test";
}

}

?>
---snip---

I would expect the "echo ..." line to display "Test", but it doesn't. I
swear I must be missing something really simple and really stupid, but I
can't find it for the life of me. Little help? Oh, PHP Version 4.3.4 if it
makes a difference.

Thanks in advance,
'Mutt
Jul 17 '05 #1
7 1705

"Shuttermutt" <an*******@nospam.net> wrote in message
news:Op*******************@twister.rdc-kc.rr.com...
Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to OOP in general. I've spent the last three hours trying to figure out why this
code snipped doesn't do what I expect it to:

Try it the other way around.
<?php
class Test
{

// Class constructor
function Test()
{
// Empty on purpose...
}

// Returns class name
function GetClassName()
{
return "Test";
}

} $myTest = new Test;

echo $myTest->GetClassName; // Nada
?>


Note the class is b4 the calling of the class.

Simon.
Jul 17 '05 #2
Shuttermutt wrote:
Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to OOP
in general. I've spent the last three hours trying to figure out why this
code snipped doesn't do what I expect it to:

---snip---
<?php

$myTest = new Test;

echo $myTest->GetClassName; // Nada

class Test
{

// Class constructor
function Test()
{
// Empty on purpose...
}

// Returns class name
function GetClassName()
{
return "Test";
}

}

?>
---snip---

I would expect the "echo ..." line to display "Test", but it doesn't. I
swear I must be missing something really simple and really stupid, but I
can't find it for the life of me. Little help? Oh, PHP Version 4.3.4 if it
makes a difference.
Raise your error level, and you'll have the answer (which is quite
obvious BTW, but warnings may help spot less obvious bugs...)

Notice: Undefined property: GetClassName in <filename>.php on line 5

Ok, line 5 is :
echo $myTest->GetClassName; // Nada

Err... seems like parens are missings !-) Used to be a VB coder ?-)

Try this instead :
echo $myTest->GetClassName();
Thanks in advance,

You're welcome

Jul 17 '05 #3
MyOdd wrote:
"Shuttermutt" <an*******@nospam.net> wrote in message
news:Op*******************@twister.rdc-kc.rr.com...
Okay, I'm a relative newcomer to object-oriented PHP, but no stranger to


OOP
in general. I've spent the last three hours trying to figure out why this
code snipped doesn't do what I expect it to:

Try it the other way around.

<?php
class Test
{

// Class constructor
function Test()
{
// Empty on purpose...
}

// Returns class name
function GetClassName()
{
return "Test";
}

}


$myTest = new Test;

echo $myTest->GetClassName; // Nada
?>

Note the class is b4 the calling of the class.


Err... The fact is that
- your code exhibits the same problem (you didn't tried your 'solution',
did you ?)
- the OP's code, once the *real* problem is corrected (see other post in
this thread), works quite ok.

BTW, I don't know much about the internals of PHP, but it seems that the
whole file is parsed before execution, so you don't need to put the
class definition *before* using it.

Bruno

Jul 17 '05 #4

$myTest = new Test;

echo $myTest->GetClassName; // Nada
?>

Note the class is b4 the calling of the class.


Err... The fact is that
- your code exhibits the same problem (you didn't tried your 'solution',
did you ?)


No, sorry.
- the OP's code, once the *real* problem is corrected (see other post in
this thread), works quite ok.
Ideed.

BTW, I don't know much about the internals of PHP, but it seems that the
whole file is parsed before execution, so you don't need to put the
class definition *before* using it.


Not for me, but i don't feel like looking into it.

Simon
Jul 17 '05 #5
Bruno Desthuilliers wrote:

Err... seems like parens are missings !-) Used to be a VB coder ?-)

Try this instead :
echo $myTest->GetClassName();
Thanks in advance,

You're welcome


I am SUCH an idiot! Thank you.

--
'Mutt
Jul 17 '05 #6
Uzytkownik "Bruno Desthuilliers" <bd*****************@free.quelquepart.fr>
napisal w wiadomosci news:40*********************@news.free.fr...
BTW, I don't know much about the internals of PHP, but it seems that the
whole file is parsed before execution, so you don't need to put the
class definition *before* using it.


No, in PHP class definition is a runtime operation. That's why something
like this works:

function define_cow($good) {
if($good) {
class Cow {
var $name;
var $weight;
}
}
else {
class Cow {
var $name;
var $madness_severity;
}
}
}

define_cow(false);
$obj = new Cow();
Jul 17 '05 #7
On Wed, 17 Mar 2004 12:30:16 +0100, Bruno Desthuilliers wrote:
MyOdd wrote:
BTW, I don't know much about the internals of PHP, but it seems that the
whole file is parsed before execution, so you don't need to put the class
definition *before* using it.

Bruno


That has changed as of PHP 5.

La'ie Techie

Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Howard | last post by:
Hi, in one of the recent posts, I saw someone pass two variables of built-in type (int and double), by reference, to a member function of a class. That function then took the addresses of those...
19
by: Eric Lilja | last post by:
Hello, I have a class that I want to be able to output detailed information about what it's doing either to a file or to the screen. What I first tried was adding a std::ostream-reference member...
5
by: Billy | last post by:
Hi, I would like to return a reference to a member variable: class foo { int i; int& geti() const; };
7
by: Pekus Cons. e Desenvolvimento | last post by:
Hi, I have seen some developers commenting that a reference variable is 'more garbage collection eligible' if the programmer set its value to null after using it, like this: public void...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
11
by: Richard Lewis Haggard | last post by:
Is it possible to put a reference to an object inside of a class? If so, what is the syntax? The reason I want to do this is so that I can make a copy of the original object, make modifications to...
8
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
4
by: siddhu | last post by:
If there is reference member variable in the class, why doesn't default assignment operator work? class A { int& i; public: A( int& ii):i(ii){} //A& operator=(const A& a){i = a.i;} };
26
by: Turin | last post by:
Dear all; As far as I understand the idea behind getter methods, it is used to make sure that private memers of a class is returned appropriately to the calling object. However, if all I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.