SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    classes and oop help.

    Ok, im trying to design a template system (very basic atm and please dont recomend any as i am making my own).

    Now i think ive done everything right but i am getting the following error:

    Fatal error: Call to a member function on a non-object in C:\apache2triad\htdocs\cms\test.php on line 79
    Now this is the following code i have:

    skin/1/skin_test.php
    Code:
    class skin_test {
    
    function some_text() {
    
    echo <<<HTML
    blah
    HTML;
    
      }
    
    }
    now in test.php i have:
    Code:
    include("skin/1/skin_test.php");
    $html 		= new skin_test;
    I then go down a little (into a function) and i global $html and then i have:
    Code:
    	$html->some_text();
    If anyone could point out what i am doing wrong, because i surly can't find it. Thank you very much.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Classes that don't have any member variables cannot have their functions called with the -> syntax. Instead, you have to use the scope resolution operator :: . So, for example, you'd call your function like this:

    skin_test::someText();

    *edit: Err, I confused member-less classes with uninstantiated classes. Whether a class has any members is irrelevant to being able to access its methods (unless the methods depend on there being certain vars). The scope resolution operator is for accessing methods in uninstantiated classes or parent classes.
    Last edited by k0ng; Apr 23, 2005 at 18:21.

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok well when i put that in i get the error:

    Fatal error: Undefined class name 'skin_test' in C:\apache2triad\htdocs\cms\test.php on line 79

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Uk
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try passing $html into the function and see if that works.

  5. #5
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nah didnt work

    edit* found out the problem, when i put:

    include("skin/1/skin_test.php");
    $html = new skin_test;

    into the function it works, so i have to figure out how i was trying to do it before, if you have any suggestions please let me know.

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this works, so there is something else wrong.

    PHP Code:
    class skin_test {

    function 
    some_text() {
    echo <<<HTML
    blah
    HTML;

    }

    }

    $html = new skin_test;
    $html->some_text(); 
    Douglas
    Hello World

  7. #7
    SitePoint Enthusiast DmS's Avatar
    Join Date
    Jan 2004
    Location
    Stockholm, Sweden
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <edit>Doh, wrong answer... would confuse more than help so I removed it... Sorry </edit>

    /Dan
    { knowledge is what remains once you forget what you learned }
    Home: DmSProject Tutorials: GurusNetwork
    Committed at:
    OzoneAsylum + Blog

  8. #8
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thaks for all help, i figured it out anyway, just kept playing around with things and finally worked

    Thanks, and a mod can close this topic!


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
  •