PHP, how to "include" code in the body of a class, outside a function

I have a Symfony2 controller in which I want to include a file containing common functions. However, when I use the require statement, I get an error that the request was unexpected. In addition, in PHPStorm, I also get an error that only functions can be declared where I have a require statement.

Here is an overview of my code:

<?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Response; use ReCaptcha\ReCaptcha; class MyController extends Controller { private $salt_length = 10; private $resources; private $request; private $session; private $message; private $loggedin; /** * @Route("/My/home") */ // // My home page // public function homeAction() { // bla bla } require 'utilities.php'; // <<============== } ?> 

The request at the end of my class creates a compiler error.

I would prefer not to create another package and use it, because I want the MyController scope to act when functions are called in the .php utility.

Is there another preprocessor directive that includes the contents of a file in another in order to achieve what I'm trying to do?

Oddly enough, when I look, turn on and demand that the php manual and w3schools php pages seem to say that you can use include / require to accomplish exactly what I'm trying to do here.

Thank you in advance for any help anyone can give me.

0
include php symfony
Dec 15 '16 at 1:26
source share
2 answers

What you might be looking for are traits:

http://php.net/manual/en/language.oop5.traits.php

Using tags allows you to load additional functions into your class.

 <?php namespace AppBundle\Traits; trait UtilityTrait { public function doStuff() { return 'stuff'; } } ?> 



 <?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Response; use ReCaptcha\ReCaptcha; use AppBundle\Traits\UtilityTrait; class MyController extends Controller { use UtilityTrait; .... } 
+2
Dec 15 '16 at 1:41
source share

You can put any code you want outside the class definition, and it will run when the class loads. I prefer to create a class of static helper functions instead of global functions if you want to include non-class helper functions of type that have no state.

To mark:

  • If inside utilities.php is the actual class or classes, do the right thing and register the autoloader instead.
  • Use require_once instead of require

This is messy and I do not recommend it, but:

  <?php namespace AppBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Response; use ReCaptcha\ReCaptcha; require_once 'utilities.php'; // <<============== class MyController extends Controller { .... 
0
Dec 15 '16 at 1:31
source share



All Articles