Using a Laravel's class file in non-laravel PHP

Viewed 177

I'm trying to import classes that are in Laravel into my code (to avoid duplicating classes and code and such).

However, my non-laravel app doesn't use namespaces or composer. Whenever I try to import a class from Laravel and instantiate it, I'll get an error that the class cannot be found since Laravel's files are using a namespace. I also tried initializing the class like this:

include "../app/Classes/Calendar.php";
$calendar = new Calendar();

But alas, that still did nothing, and I am getting the following error:

Fatal error: Uncaught Error: Class 'Calendar' not found in W:\xampp\htdocs\public\legacy_index.php:52 Stack trace: #0 W:\xampp\htdocs\server.php(28): require_once() #1 {main} thrown in W:\xampp\htdocs\public\legacy_index.php on line 52

Is there any way to do this without having to namespace the non-laravel code? Thank you!

Edit:

This is my folder structure:

enter image description here

I'm trying to import Calendar.php from app/classes into legacy_index.php from public/

And this is my Calendar.php

<?php
namespace App\Classes;
use DateTime;
use DateTimeZone;
use PDO;
class Calendar
{
    private $db_connection = null;
    public $errors = array();
    public $messages = array();

    public function __construct(){}
    private function databaseConnection(){}
    private function getAppointments($startdate, $enddate){}
    public function getAppointmentsJSON($start, $end){}
}
1 Answers

I was able to make it work by adding the namespace in the intialization of the class:

include "../app/Classes/Calendar.php";
$calendar = new App\Classes\Calendar();
Related