473,624 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP Static Class Properties

72 New Member
Hello,

I should have made a note of which version of php 5 I am currently using, but I didn't (I'm pretty sure it's 5.2.2).

Ayway, I just want to clarify that my understanding of the static keyword under php is correct, as I tried something earlier that I would have expected to work in any other OOP language, and it didn't.

Am I correct in stating:

a) Static class properties cannot be assigned values after declaration.
b) Static variables cannot be assigned an object data type, only primitives.
c) Static class properties cannot be accessed by an instantiated object through the $this notation.

I tried implementing something similar to the code below, which I would have expected to work, but it seems php handles static slightly differently to other languages.

Regards,
Rob.

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.   //Various instance variables and methods.
  4. }
  5.  
  6. class B
  7. {
  8.   //Did not have a problem with either of the below lines.
  9.   protected static var;
  10.   protected static flag = false;
  11.   //Did have a problem with this though:
  12.   //protected static var = new A;
  13.  
  14.   public function __construct()
  15.   {
  16.     if(!self::flag) {
  17.       //Had a problem with both the following lines.
  18.       self::var = new A;
  19.       self::flag = true;
  20.     }
  21.   }
  22. }
  23.  
  24. class C extends B
  25. {
  26.   public function __construct()
  27.   {
  28.     parent::__construct();
  29.   }
  30. }
  31.  
Nov 4 '07 #1
2 6118
pbmods
5,821 Recognized Expert Expert
Heya, Peridian.

Only point 3 is correct; you don't use $this to access static properties:

Expand|Select|Wrap|Line Numbers
  1. class Tester
  2. {
  3.     protected static $myVar;
  4.  
  5.     public static function getVar($val)
  6.     {
  7.         return self::$myVar;
  8.     }
  9.  
  10.     public static function setVar($val)
  11.     {
  12.         return self::$myVar = $val;
  13.     }
  14. }
  15.  
  16. class Outputter
  17. {
  18.     public function __tostring()
  19.     {
  20.         return 'Outputter';
  21.     }
  22. }
  23.  
  24. Tester::setVar('Hello, World!');
  25. echo Tester::getVar();    // 'Hello, World!'
  26.  
  27. Tester::setVar(new Outputter());
  28. echo Tester::getVar();    // 'Outputter'
  29.  
Nov 4 '07 #2
peridian
72 New Member
Ahh, awesome. I can't believe it but when I checked my syntax in the code, I had dropped the $ from after the :: part. I think I must have thought I was using a static method call or something.

EDIT: Doh, don't worry, just answered my own question. Thanks a lot.
Nov 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
2218
by: Mike P | last post by:
I have a class with a dozen+ properties, some of which will be set a value, and some not, depending on the constructor called. I also have a method which has only one overload and all of the properties are being sent to it. My question is, because some of these properties will not be initialised by the constructor used, calling this method in some cases will crash my app unless I initialise all the properties at some point in my class....
25
5152
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I wanna use global.asax) --- Would you declare your static var as --- public static int x ;
11
3814
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
6
2038
by: RSH | last post by:
I'm having a bit of trouble understanding Static properties. I have two forms and I need to be able to access a property from from1 in form2. The code below does not work but I'm not sure why not. And also why is it necessary to declare the variable bIshShown twice? Form1 code:
11
11156
by: Raja Chandrasekaran | last post by:
Hai folks, I have a question to get exact answer from you people. My question is How Static class is differ from instance class and If you use static class in ASP.NET, ll it affect speed or performance of site...? Because I am using static class in my Database layer. But one of my friend told me that "static class ll take more space than instance class and it ll affect the performance. But if you use Instance class
8
8920
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
7
13643
by: wagger | last post by:
Hi! I am creating a set of static classes, one class for each "case" I have. The class should do and perform certain things, and the different static classes are doing different things. However, in each static class there must be 3 properties (id, name and description) and I thought I just create an abstract class and define them there and let all the static classes (ie the cases) inherit from that and thus forcing them to implement the...
5
4453
by: Dave | last post by:
Hello, Suppose you have a class with a static property with only a get (read only). You also have code in a static constructor that sets these properties but takes 1 hour to run. Now suppose the first request comes in at 11:00 am and tries to read from this property. It will need to wait an hour until the page loads which is fine and makes sense. If a second (different) request at 11:01 am comes in and tries to read the same...
12
4978
bilibytes
by: bilibytes | last post by:
hi, i need some help here. how can i access a static property of a subclass. i have an abstract class which will be extended... each extending class will have a static property that will store an array like this: protected static $_multiLevelArray = array('one'=> array(...), 'two'=> array(...)) protected static $_finalArray;
0
8168
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8672
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8614
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8471
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7153
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6107
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4075
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4167
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2603
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.