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

Let's say I've got two files class.php and page.php

class.php

data = get_data('mydata');
        }
    }
?>

That's a very rudamentary example, but let's say I want to use:

$vars = new IUarts(); 
print($vars->data);

in my page.php file; how do I go about doing that? If I do include(LIB.'/class.php'); it yells at me and gives me Fatal error: Cannot redeclare class IUarts in /dir/class.php on line 4

asked Jan 16, 2013 at 3:12

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

XhynkXhynk

13.1k8 gold badges32 silver badges69 bronze badges

You can use include/include_once or require/require_once

require_once('class.php');

Alternatively, use autoloading by adding to page.php

data);    
?>

It also works adding that __autoload function in a lib that you include on every file like utils.php.

There is also this post that has a nice and different approach.

Efficient PHP auto-loading and naming strategies

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

Chuck Le Butt

46.3k59 gold badges192 silver badges281 bronze badges

answered Jan 16, 2013 at 3:20

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

lmcanavalslmcanavals

2,2931 gold badge22 silver badges34 bronze badges

2

In this case, it appears that you've already included the file somewhere. But for class files, you should really "include" them using require_once to avoid that sort of thing; it won't include the file if it already has been. (And you should usually use require[_once], not include[_once], the difference being that require will cause a fatal error if the file doesn't exist, instead of just issuing a warning.)

answered Jan 16, 2013 at 3:14

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

Ry-Ry-

211k54 gold badges441 silver badges455 bronze badges

Use include_once instead.
This error means that you have already included this file.

include_once(LIB.'/class.php');

answered Jan 16, 2013 at 3:14

Use include("class.classname.php");

And class should use not

answered Nov 30, 2015 at 4:43

2

use

require_once(__DIR__.'/_path/_of/_filename.php');

This will also help in importing files in from different folders. Try extends method to inherit the classes in that file and reuse the functions

answered Oct 25, 2019 at 4:59

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

Adnan FayazAdnan Fayaz

1112 silver badges8 bronze badges

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.

How can I call a class from another file in PHP?

Use include("class. classname. php");

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 can I access a class in PHP?

Creating an instance of a class And then you can access all of the class's functionality — via your instance — using the familiar arrow, -> , syntax: // creates an instance of the Html class $myPage = new Html; // prints

Whoomp, there it is!

$myPage->printParagraph('Whoomp, there it is!

How do I join two PHP files?

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement. The include and require statements are identical, except upon failure: require will produce a fatal error (E_COMPILE_ERROR) and stop the script.