Can you instantiate a class from another project?

-1

I would like to know if it is possible to make the call of a function that is in a class of another project.

This is the structure of both projects:

.
├── Proyecto 1 (PHP)
│   └── CarpetaDeClases
│       └── ClaseALlamar
│
└── Proyecto 2 (LARAVEL 5)
    └── App
        └── Http
            └── Controllers
                └── ArchivoLlamaClase
    
asked by Santiago Muñoz 22.09.2016 в 18:56
source

2 answers

1

The short answer with the information you give is YES .

  

I do not know Laravel, and it will have its peculiarities or facilities, but being PHP, it is quite standardized.

As an instance the class?

Well, you have two options depending on the configuration:

1.- Namespace

Only if the project configuration includes Proyecto 1 within the autoload.
And if the class to call has specified the namespace .

use Proyecto_1\CarpetaDeClases\ClaseALlamar;

class ArchivoLlamaClase
{
    public function loquesea() {
        $clase = new ClaseALlamar();
        // también directamente sin usar el operador use
        $clase = new \Proyecto_1\CarpetaDeClases\ClaseALlamar();
    }
}

2.- Through require or include

The second option is to include it through include or require depending on your interest.

require dirname(__DIR__) . '/../../../Proyecto_1/CarpetaDeClases/ClaseALlamar.php';

class ArchivoLlamaClase
{
    public function loquesea() {
        $clase = new ClaseALlamar();
    }
}

It is a matter of establishing the correct routes and having the error report activated while you edit the classes to observe the notifications in case of throwing an error.

If in spite of having the routes correctly throws errors, you have to go in to see permissions, configuration and other parameters of the server.

    
answered by 22.09.2016 в 19:23
0

Example (A as I understand you):

Assuming you have a Controller:

namespace App\Http\Controllers;

class PagesController extends Controller
{

  public function getHome()
  {
    return view('pages.home');
  }

}

And you want to use the following class:

class PricesClass {
  public function getPrices() {
    return ['bronze' => 50, 'silver' => 100, 'gold' => 150];
  }
}

You can put a class itself anywhere you like within the \ App

folder

Saying this, in this example, you can save the class as APP \ Classes \ PricesClass.php . Then you add the namespace App \ Classes .

Then the use of the class would be like this:

namespace App\Http\Controllers;

use App\Classes\PricesClass;

class PagesController extends Controller
{

  public function getHome()
  {
    $pricesClass = new PricesClass();
    $prices = $pricesClass->getPrices();
    return view('pages.home', compact('prices'));
  }

}

Another option

For example you have this archivo.php which is not a class or anything, just a function:

<?php

function getPrices() {
  return ['bronze' => 50, 'silver' => 100, 'gold' => 150];
}

You can use PHP functions such as include () or require () without forgetting the app_path () function.

Staying as follows:

 public function getHome()
    {
      include(app_path() . '\functions\archivo.php');
      $prices = getPrices();
      // ...

You can check the original source for a better understanding.

    
answered by 22.09.2016 в 19:20