1

I'm running PHP 5.6.24 and as far as I understand, I should be able to do this:

functions.php

<?php
namespace lib;

function test_function ($var) {
    echo $var;
}
?>

test.php

<?
require 'functions.php';

use lib\test_function;

test_function('Hello, world!');
?>

However, I get a Fatal error: Call to undefined function test_function()

What am I doing wrong?

7
  • if you're not sure what version of php you are running go ahead and write a script that just has phpinfo(); in it. and look through it. Commented Aug 7, 2016 at 4:12
  • I have PHP 5.6.24. Commented Aug 7, 2016 at 4:17
  • im pretty sure you have to give the file a namespace also. Commented Aug 7, 2016 at 4:18
  • Look at the first example: php.net/manual/en/language.namespaces.basics.php Commented Aug 7, 2016 at 4:19
  • 2
    php.net/manual/en/language.namespaces.importing.php Commented Aug 7, 2016 at 4:25

1 Answer 1

2

If you would like to use a function, you have to do something like this:

use function lib\test_function as func;
func("Hello World!");

Hope this helps!

Supporting Link!

Sign up to request clarification or add additional context in comments.

1 Comment

That was it! I needed to add the keyword "function" after "use." Thank you!

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.