Welcome Guest, Not a member yet? Register   Sign In
Static object inside class
#1

[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!
#2

[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 (->)
#3

[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()?
#4

[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()
#5

[eluser]m4rw3r[/eluser]
More like:
Code:
class Foo
{
    public func() {}

}

class Bar
{
    public static foob;
    public Bar()
    {
        Bar::$foob = new Foo(); // or you can use self::$foob in PHP 5
    }
}
#6

[eluser]Colin Williams[/eluser]
Bah! Too early for me to be making suggestions Smile 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
#7

[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.
#8

[eluser]Jonathon Hill[/eluser]
There are typos in your class. It should be:

Code:
class Foo
{
    public function func() {}
}

class Bar
{
    public function Bar() { self::$foob = new Foo(); }
    public static $foob;
}

# call like this
Bar::$foob->func();




Theme © iAndrew 2016 - Forum software by © MyBB