SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Call to a member function on a non-object

    PHP Code:
    class {
        function 
    test() {
            return 
    'good';
        } function 
    test2() {
            
    $t $this->test();
            echo 
    $t;
        }
    }
    A::test2(); 
    Hello, I have a pretty basic question (I think). Running the code above in PHP 4.4.1 results in the following:
    Fatal error: Call to a member function on a non-object in /home/americi1/public_html/taylor/test/test.php on line 6

    I have a class with many functions and some of those functions refer to other functions as seen above. Does anyone know how to amend my existing code or have a better idea?
    Thanks
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  2. #2
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're calling test2() as static method on class A. In test2() you're trying to call test() on an instance of A, which fails, because there is not instance. A quick fix to this is to change $t = $this->test() to $t = self::test(), or maybe $t = A::test(). Don't know if PHP4 has 'self' keyword.

    Second solution is to create an instance of A and then call test2() as a method on the object:
    PHP Code:
    $a = new A();
    $a->test2(); 
    But in the long run, you should clear up your confusion with classes and objects.

    Edit: added second solution.

  3. #3
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •