473,396 Members | 1,713 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,396 software developers and data experts.

PHP Static Class Properties

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 6108
pbmods
5,821 Expert 4TB
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
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
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...
25
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...
11
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...
6
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...
11
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...
8
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....
7
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,...
5
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...
12
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.