Static object inside class |
[eluser]venksster[/eluser]
OK this is not a CI related query per se, but since this section said "anything related to programming", i decided to post... this is similar to what i have as my libraries (application/libraries): class Foo { public func() {} } class Bar { public Bar() { foob=new Foo(); } public static foob; } ---------------- Now I wish to reference the func() function inside foob object through bar class I tried Bar::$foob->func() // Doesnt work Bar::$foob.func() // Doesnt work Any ideas? Thanks in advance!
[eluser]venksster[/eluser]
When i say, doesnt work, my interpreter error is Fatal error: Call to a member function func() on a non-object for the first one (->)
[eluser]Phil Sturgeon[/eluser]
Try without the $. You have assigned it as static, not a property. Failing that, you could extend Foo, or call it with call_user_func()?
[eluser]Colin Williams[/eluser]
$this $this $this You need to set a member variable of the class, not a local variable of the member function. You need $this->foob = new Foo()
[eluser]m4rw3r[/eluser]
More like: Code: class Foo
[eluser]Colin Williams[/eluser]
Bah! Too early for me to be making suggestions I do think you need the $ in the public static declaration. Everything I'm seeing on PHP.net shows it with the $ http://us.php.net/language.oop5.static
[eluser]venksster[/eluser]
Thats true, Colin, I think I need the $. And yes, i had actually used the $this. I also tried Bar::$foob = new Foo(); as per m4rw3r's suggestion...but doesnt work... m4rw3r, could you tell me how to actually CALL the func? is it Bar::$foob.func() ? (doesnt work with -> either) buti have a feeling that the class code is fine. its how i call the function thats screwed up.
[eluser]Jonathon Hill[/eluser]
There are typos in your class. It should be: Code: class Foo |
Welcome Guest, Not a member yet? Register Sign In |