SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Photoshop Ninja jonnya's Avatar
    Join Date
    Jan 2002
    Location
    Birmingham UK
    Posts
    733
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a function in a class thats in an include in WordPress theme functions.php

    Hi - I have just started to get my head around PHP classes (finally!) and have hit a bit of a block.

    I am working in WordPress and have a 'functions.php' file in my template. I have various classes that seem to work just fine so far and for neatness I have started separating these out into includes.

    I can call a function from one of my classes in my include just fine in the WordPress backend (it builds an admin panel).

    However, when I try and call a function from one of my includes defined in functions.php - it doesn't work!

    I have a simplified version below that will hopefully explain this a-little better:

    Code in class/myinclude.php

    PHP Code:
    <?php

    class MyDisplay {

        function 
    templateoutput() {
            echo 
    "hello";
        }

    }
    ?>
    Code in functions.php (in template folder)

    PHP Code:
    <?php include("class/myinclude.php"); ?>
    Code in my template:
    PHP Code:
    <?php if (function_exists("MyDisplay::templateoutput()")) {MyDisplay::templateoutput();} ?>
    I have tried a few things (along with quite a bit of Google research!). I have tried making the function public (I was under the impression if you didn't imply this all functions are public), this didn't work anyway!

    Any help would be appreciated thank you!
    Jonnya Freelance Creative
    UK Freelance designer and web developer
    Website | Portfolio | Photoshop Tips

  2. #2
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If no scope is defined (public, private, protected) it's default is public for the function.

    Have you tried it this way:
    PHP Code:
    $display = new MyDisplay();
    $display->templateoutput(); 
    Is it getting called if you do that?

  3. #3
    Photoshop Ninja jonnya's Avatar
    Join Date
    Jan 2002
    Location
    Birmingham UK
    Posts
    733
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JREAM - Thanks for confirming the scope thing, I thought it was public if not declared.

    Do you always have to always create a new instance of a class to use a function in it (as per your example) - sorry if that sounds a bit dim, as I said I am just getting into classes!!

    I have used the protocol to call a function in a class before:

    PHP Code:
    MyDisplay::templateoutput() 
    and it worked... this class stuff is confusing!!
    Jonnya Freelance Creative
    UK Freelance designer and web developer
    Website | Portfolio | Photoshop Tips


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
  •