0

I'm trying to access to public static method inside of php class save in separate file from another file but I getting the error

"NetworkError: 500 Internal Server Error - http://localhost/web/test.php"

Model.php

<?php
class Model
{
    public static function mensaje()
    {
        return "Mensaje";
    }
}

test.php

<?php
$mensaje = Model::mensaje();
echo $mensaje;

If I put the Model class in the same file of test.php work perfectly, but not when I put in separate files.

Model.php and test.php are in the same directory.

How can I access propertly to static method from other file?

3
  • 3
    Are you include Model.php file? Jan 31, 2015 at 17:23
  • Just because a Model.php file exists, doesn't that mean it's automagically included in your test.php, unles you're using an autoloader
    – Mark Baker
    Jan 31, 2015 at 17:25
  • public static function in php are not global for entire project?
    – gvd
    Jan 31, 2015 at 17:29

2 Answers 2

2

In test.php you'll need to include or require the Model file so it knows where Model is defined:

<?php
require_once('Model.php');
$mensaje = Model::mensaje();
echo $mensaje;

http://php.net/manual/en/function.require-once.php

0
0

You just need to include or require the file in your current file you are using

 <?php
 Require'test.php';
 //Or you can use  
 Include'test.php'; 
 // both will work 

 ?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.