Jump to content

Recent Status Updates

  • Photo
      16 Sep

    Some spammers sign up to CC and then they leave their account hidden so they think it won't get noticed but with an obvious name like "SaxophoneRetailSingapore" it's hard not to know they are a spam bot. #ModLife

    Show comments (3)
  • Photo
      15 Sep

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (1)
  • Photo
      12 Sep

    Just joined. Really enjoy this sight. Excited to become a great programmer, and helper.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
- - - - -

Fatal error: Access to undeclared static property


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 alirezan


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 74 posts

Posted 16 April 2013 - 07:25 PM

Hi guys


I'm fairly new to PHP OO and I would really like some help.


I have a class defined like this:

class testContainer
const $test0 = 0;
const $test1 = 1;
const $test2 = 2;
const $test3 = 3;
const $test4 = 4;
const $test5 = 5;

Then at a later point in another class in the same file I have:


class testContainerTester
public function test1 ($va)
echo testContainer::$va;

$tct = new testContainerTester;
echo $tct->test1("test5");



I get this error:

"Fatal error: Access to undeclared static property: testContainer::$va in"



I don't understand this. Can someone please help explain why this is happening and how I can fix it?


Thanks so much

Edited by alirezan, 16 April 2013 - 09:00 PM.
code tags

#2 Harold


    CC Regular

  • Member
  • PipPipPip
  • 27 posts

Posted 16 April 2013 - 08:29 PM

it says fatal error because it cant access testContainer::$var. test container should be declare static so that it can be shared and accessible by the other methods/class/objects.

#3 alirezan


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 74 posts

Posted 16 April 2013 - 09:21 PM

I did and it still doesn't work :( Any suggested code perhaps please?


Thanks so much

#4 BlackRabbit


    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,358 posts

Posted 16 April 2013 - 11:09 PM

I think you fail to understand the difference between class and instance.


Class is the class declaration, and instance is a class implementation, in easy words, and instance is a variable of the type testContainer, which is non existent in testContainerTester, where you are trying to get at the class definition instead of the class instance.


so you just need to declare a variable of the type testcontainer inside the tester and then you can work the way you want to.

#5 JasonKnight


    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 312 posts

Posted 18 April 2013 - 08:50 PM

Class constants don't/can't have $ before them. Also if a bunch of const or properties are getting the same protoype (const, public, private, protected, etc) you can just say the prototype ONCE then separate them with comma's.

Your biggest problem though, saying:

echo testContainer::$va;

is the same as saying testContainer::$test5

Which since constants can't have $ before them you can't do it that way.

The solution? http://us3.php.net/m...on.constant.php

So to do what you are asking:

class testContainer {
		test0 = 0,
		test1 = 1,
		test2 = 2,
		test3 = 3,
		test4 = 4,
		test5 = 5;

class testContainerTester {
	public function test1 ($va) {
		echo constant('testContainer::'.$va);

$tct = new testContainerTester;
echo $tct->test1('test5');

Works like a charm...

Edited by JasonKnight, 18 April 2013 - 08:51 PM.

The only thing about Dreamweaver that can be considered professional grade tools are the people promoting it's use.