How can i call a class from another file in php?

Like this article? We recommend

Importing Classes from a Separate File

Although it's useful to modularize our code the way we have, it's best if every page that refers to events does it through Event objects. So for maintenance purposes, it's best to move the class definition into a separate file that we can call from different pages. To start, create a new file—I'll call mine objects.inc, and I'll place it in the same directory as showevent.php—and save it. Place the class definition in that file, as shown in Listing 5.

Listing 5—A Separate Class Definition File (objects.inc)

eventid = $this_eventid;
      $this->month = $event["eventMonth"];
      $this->day = $event["eventDay"];
      $this->year = $event["eventYear"];
      $this->title = $event["eventTitle"];
      $this->description = $event["eventDesc"];
    }

    // Free resources.
    mysql_free_result ( $results );

  }
}
?>

Notice that the entire section is enclosed in the delimiters.

To actually use the object, we need to make the class definition available from within the page. To do that, we can use the require() function, as shown in Listing 6.

Listing 6—Making the Class Definition Available (showevent.php)

  require("objects.inc");

  import_request_variables('pgc', '');

  $this_event = new Event($eventid);

  printf ( "

%s

", $this_event->title ); printf ( "

Date: %s/%s/%s

", $this_event->month, $this_event->day, $this_event->year ); printf ( "

%s

", $this_event->description ); printf ( "Edit this event", $eventid ); ?>

The require() function acts as if all of the code in the objects.inc file were included in this file at that point.

Four functions enable you to include code from another file: include(), require(), include_once(), and require_once(). All four can take a local file or URL as input. The difference between the include and require functions is that include() and include_once() provide a warning only if the resource can't be retrieved; whereas require() and require_once() stop processing the page. Because we need the class definition to proceed, we're using require().

The include_once() and require_once() functions are handy in situations where multiple files may reference the same included code; if the file has already been included, it won't be included again. Because a function can't be redefined once it's declared, this restriction can help prevent errors.

 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.


 • This site uses cookies - see [here] for details.
 • If you proceed, we will take that as your consent to accept cookies

Loading in a class from another file and using it

Objects in PHP example from a Well House Consultants training course
More on Objects in PHP [link]

This example is described in the following article(s):
   • Shipping a test harness with your class in PHP - [link]

Source code: ob2.php Module: H108

include "ob1.php";

$ours = new square(6,7);
$a = ($ours->getarea());

?>


Using a class from another file


Application Shell


This script loads in a class from another file and creates and uses a simple
obeject from it, coming up with the answer .


Significantly for this demonstration, the class file includes a test harness
which (if it is run alone) produces a whole page of results.

Learn about this subject

This module and example are covered on the following public courses:
 * Object Oriented Programming in PHP
 * PHP Programming
 * Learning to program in PHP
Also available on on site courses for larger groups

Books covering this topic

Yes. We have over 700 books in our library. Books covering PHP are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples

This example comes from our "Objects in PHP" training module. You'll find a description of the topic and some other closely related examples on the "Objects in PHP" module index page.

Full description of the source code

You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources

• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
• The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Web site author

This web site is written and maintained by Well House Consultants.

Purpose of this website

This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Conditions of use

Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

Further Examples

Related Material

How can use class from another class in PHP?

The public keyword is used in the definition of the class, not in a method of the class. In php, you don't even need to declare the member variable in the class, you can just do $this->tasks=new tasks() and it gets added for you.

How do you call a class in PHP?

PHP: Class Constants When calling a class constant using the $classname :: constant syntax, the classname can actually be a variable. As of PHP 5.3, you can access a static class constant using a variable reference (Example: className :: $varConstant).

What is difference between require and include in PHP?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

What is the use of use keyword in PHP?

The use keyword has two purposes: it tells a class to inherit a trait and it gives an alias to a namespace.