There is such a problem: there is a class, and in it methods. But when I try to call these methods, php gives an error that there are no such methods. Although IDE, for example, sees everything.

Here is the class:

<?php class Config { private static $config = null; private $host = 'localhost'; private $userName = 'root'; private $password = ''; private $database = 'keep_lab'; private $lcTimeNames = 'ru_RU'; private $encoding = 'utf8'; public static function getConfig() { if (self::$config === null) { self::$config = new self; } return self::$config; } } 

The method is called in another class.
Classes are separated by different files and are in the same directory.
class sources

Here is the method call:

 <?php include_once 'Config.php'; class DataBase{ // лишнее вырезано public static function getDB() { if (self::$db === null) { self::$db = new self(Config::getConfig()); } return self::$db; } } // .... $db = DataBase::getDB(); 

Error text:

 Fatal error: Call to undefined method Config::getConfig() in D:\Sergei\Programming\Web-programming\Keep_Notes\www.notes.lb\lib\DataBase.php on line 21 
  • You have only one method called getConfig() . It is called without error. Everything else is private properties that should not be accessible from outside ( private ). So please give a sample code that reproduces the error and post the PHP error message as it is. Otherwise, without explicitly describing the problem, the question may be confused or closed. - Streletz
  • If you add any public method to this class and call it, it will work. - Klym
  • @MaksimKlimenko, and so it works, although it does not have access to data from outside. The author's problem is different, but it is not reproduced. - Streletz
  • Resolving this issue on cyberforum-e - Maxim Timakov

2 answers 2

Honestly, the fact that you, without thinking in terms of programming, decided to pull the methods themselves and bring not all the code, but only its snippets, plays far not in your favor. And, perhaps for this, I have not found meaning in your code.

This is how it works for me:

 class Config { private static $config = null; private static $db = null; private $host = 'localhost'; private $userName = 'root'; private $password = ''; private $database = 'keep_lab'; private $lcTimeNames = 'ru_RU'; private $encoding = 'utf8'; private function __construct($config=null) { if ($config==null) { $config = $this->host . '/' . $this->userName . '/' . $this->password . '/' . $this->database . '/' . $this->lcTimeNames . '/' . $this->encoding; } return $config; } public static function getConfig() { if (self::$config === null) { self::$config = new self; } return self::$config; } public static function getDB() { if (self::$db === null) { self::$db = new self(Config::getConfig()); } return self::$db; } } $db = Config::getDB(); 

Your error could not be reproduced.

    Reproduced on Windows + PHP 5.6.3 (from the XAMPP suite)
    Not playable on Linux + PHP 7.0.9

    The method of "scientific spear" came to the following example

    There are 3 files:

    • A.php - contains class A,
     <?php class A { public static function funcA() {} } 
    • B.php - contains class B, connects A.php and calls class A methods.
     <?php include_once 'A.php'; class B { public static function funcB() { A::funcA(); } } 
    • run.php connects B.php and calls class B methods
     <?php include_once 'B.php' B::funcB(); 

    Let this case be in the folder /path0/1/
    An error does not occur if the current directory ( CWD ) coincides with /path0/1/ when starting php run.php and occurs if CWD != /path0/1/
    To reproduce the error, you need to go, say, to the level up cd .. and run php 1/run.php

    It is "treated" by connecting the A.php file by the absolute path

     require_once __DIR__. DIRECTORY_SEPARATOR .'Config.php';