473,543 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static method to get class variable (PHP4)

Hi

In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:

<?php
class baseclass
{
function get_static($var name) {
return $$varname; // here's my problem
}
}

class childclass extends baseclass
{
var $types = array('text', 'password', 'hidden');
}

print_r(childcl ass::get_static ('types'));
?>

This throws a notice:
-> Notice: Undefined variable: types in test.php on line 5

Some modifications I tried on the return statement:

return $this->$varname;
-> Notice: Undefined property: this in test.php on line 5

return self::$varname;
return self::$$varname ;
return self::${$varnam e};
-> Parse error: parse error, unexpected ';', expecting '(' in test.php
on line 5

I encountered that the same errors occur if no inheritance is used, this
does not work either (with the same modifications as above):

<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childcl ass::get_types( ));
?>

I wonder if I try to do something inappropriate, or if there is just
something I am missing?

--
Markus
May 31 '06 #1
4 12322

Markus Ernst wrote:
In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:
You probably mean private/protected instead of static.
<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childcl ass::get_types( ));
?>


The variable $types does not exist until an instance of this class is
made:

$foo = new childclass();
$foo->get_types();

May 31 '06 #2
Sjoerd schrieb:
The variable $types does not exist until an instance of this class is
made:


Thank you - so I misunderstood the concept of static variables; I
assumed that if methods can be called statically this was also possible
for properties.

--
Markus
May 31 '06 #3

"Markus Ernst" <derernst@NO#SP #AMgmx.ch> wrote in message
news:44******** **@news.cyberci ty.ch...
| Sjoerd schrieb:
| > The variable $types does not exist until an instance of this class is
| > made:
|
| Thank you - so I misunderstood the concept of static variables; I
| assumed that if methods can be called statically this was also possible
| for properties.

no, i think you understand them correctly. however, ifaik, they are only
supported starting in php 5. sjoerd's post is appropriate for php < 5.

hth,

me
May 31 '06 #4
no
Sjoerd wrote:
Markus Ernst wrote:
In order to make static class variables accessible in a clean way I try
to write a method in the base class, as shown in this example:


You probably mean private/protected instead of static.
<?php
class childclass
{
var $types = array('text', 'password', 'hidden');
function get_types() {
return $types;
}
}

print_r(childcl ass::get_types( ));
?>


The variable $types does not exist until an instance of this class is
made:

$foo = new childclass();
$foo->get_types();


anyway, the code from above will still not work until you change
function to return:

function get_types() {
return $this -> types;
}
May 31 '06 #5

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

Similar topics

1
3661
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab, turned in, graded and passed so I'm not trying to get someone to do my lab assignments, but after I got this back I was reading about the...
7
15571
by: Edward Diener | last post by:
This simple code example gives me the message, "TypeError: 'staticmethod' object is not callable". class X(object): def Y(x): print x Y = staticmethod(Y) ad = { 1 : Y } def Z(self): self.ad(3)
4
1857
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from "http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfstaticpg.asp". I opened up two instances of my browser to simulate two users accessing the page at the same time by pointing to Employee.aspx...
28
4580
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. Would the performance be better using Static versus Dynamic. I would think it would be quicker with STATIC declarations since the variables would...
44
2801
by: John A. Bailo | last post by:
Dr. Dobbs has a /glowing/ article on Ruby on Rails this month. What do you guys think? Can it replace .net, php and java? And be the Open Source OOP web solution that is not bound to Sun or MS? http://media.rubyonrails.org/presentations/pursuitofbeauty.pdf
8
2605
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new Object(); ...// some codes
55
6155
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
37
5427
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the...
0
493
by: Joe Strout | last post by:
Hi Luis, A static variable IS encapsulation. Encapsulation happens at many levels: module, class, instance, and (in languages that support it) method. A static local variable is simply the finest level of encapsulation. (Well, actually you could go one finer in some languages and have block-level static scope.)
0
7410
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7744
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7350
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7691
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5887
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5276
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4896
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.