accessing method of another class

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
welliam
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Mar 02, 2018 1:28 pm

Fri Mar 16, 2018 12:44 pm

I see an example have this code, a class in a file called database.php

Code: Select all

class Database {
  public function query($sql) {
    //code
  }
}
then in another file user.php it has this class

Code: Select all

class User {

  protected $db;
  
  public function __construct(Database $db){
    $this->db = $db;
  }
  
  public function create(array $data) {
    $this->db->query('code...');
  }
in user.php we dont use require or include so how we can reference "query" method from another class file like this ?

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Fri Mar 16, 2018 1:52 pm

Most people will include/require their file resources in a loader.php script file. And then they will include the loader.php file in all of their scripts. But of course this can be a bit wasteful time-wise if you have a lot of files and you only need specific ones for the execution of a php script. So the next more advanced thing people will do is selectively load php files using if- or switch- statements (conditional-loading) based on functionality needed. At least that's what I've done in the past. My current way is what I think the best: use function "spl_autoload_register". I don't use outside frameworks/libraries for the most part.

Check out the php docs on this function; this is the ticket I think.
http://php.net/manual/en/function.spl-a ... gister.php

Read about what this function does and then also, to expand on what I'm saying here I had shared my experience/process with the function by making a post to that page. To find my post on that page, search for "Rnealxp"

To answer your question, the files must be loaded one way or another. How and where can vary. A popular PHP dependency manager is called "composer". This tool can be used on its own or may be used within a broader PHP framework. See this url: https://getcomposer.org/

welliam
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Mar 02, 2018 1:28 pm

Fri Mar 16, 2018 3:55 pm

to use both user and database classes I can create a third file and use include for both classes but is it correct to use a method "query" inside user.php without any include or require ?

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Fri Mar 16, 2018 5:17 pm

That is correct. I went through this same process as you when I got started with PHP. You may be able to google-up a couple good articles that really will help you wrap your brain around all this. The simplest way to summarize the results of having includes/requires in your php scripts is that the end result of all of the includes is a single script that has all of the resources needed to execute during the script's life on the current server request. Think of includes as copying-and-pasting from one script file into another one. The include statement gets replaced with the code in the referenced file. This lets you re-use your code, and let's you keep your code organized based on function.

welliam
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Mar 02, 2018 1:28 pm

Fri Mar 16, 2018 7:21 pm

thanks phpRob. I tried now and I was able to use spl_autoload_register

Post Reply