How to use one class functions in another?

Asked

Viewed 737 times

1

In a project I was using a class to define functions for the entire project, and as the project is growing the need arose for the creation of a new file with new class and function.

In this case I need to consume the class which makes the database connection in the first file. How can I do this?

  • Show the problem better

  • The simplest way to do it is in the second file to make a include/require of the connection file and create a class object.

2 answers

1


Like the @rray said, in your new file use the methods mentioned:

NovaClass.php:

require 'caminho/do/arquivo/de/conexao/arquivo.php'
require 'caminho/do/arquivo/de/funções/arquivo.php'

class NovaClass {

}
  • It worked perfectly for the current goal.

  • I would add this option also in my studies I ended up facing the issue of inheritance that also helps in my initial doubt. PHP

0

You can use include/include_once/require/require_once in the file you will consume. http://php.net/manual/en/function.require-once.php

To use methods of one class, within methods of another class, you just need to instantiate the desired class and make normal use of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.