Jump to content

Recent Topics

Recent Status Updates

  • Photo
      15 Nov

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
- - - - -

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,582 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.

Powered by binpress