0

Here's an example of what I have going on:

common.php

<?php
class Common {
    function test() {
        echo 'asdf;
    }
}?>

webpage.php

<?php 
require_once("common.php");
?>

<html>
<body>
    <?php test(); ?>
</body>
</html>

No matter what I've tried, I cannot get the function test to print any text to the page. With the actual webpage I was using, anything below the '' line didn't load with that portion included. I've been searching around for the past hour to figure this out, what am I doing wrong?

1
  • you don't have a function. You have a method in a class. You never instantiate that class or call the method. You're trying to call an undefined FUNCTION. $foo = new Common; $foo->test() is what you're missing. And since you don't mention getting a "fatal error: call to undefined function" error, you're undoubtedly working with error_reporting and display_errors turned off. they should NEVER be off on a devel/debug system. Commented Jun 28, 2016 at 21:56

2 Answers 2

3

You have missing a ' closure also dont need a class for this, you should have just the function definition

<?php
function test() {
    echo 'asdf';
}
?>
Sign up to request clarification or add additional context in comments.

Comments

3
<?php
class Common {
     function test() {
        echo 'asdf'; // missing a ' closure added
    }
}?>

You can access this function using a object of this class

<?php 
require_once("common.php");

// instantiate the class before you use it.

$common = new Common(); // Common is a object of class Common
?>

<html>
<body>
    <?php echo $common->test(); ?>
</body>
</html>

Alternatively, If you don't want to have a $common variable you can make the method static like this.

<?php
class Common {
    static function test() {
        echo 'asdf';
    }
}?>

Then all you have to do to call the method is:

<html>
<body>
    <?php echo Common::test(); ?>
</body>
</html>

1 Comment

dminones answer is also valid. Judging by the names used, this was only test code and not the actual intended use. It really depends what you are trying to achieve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.