Search code examples
phpsymfony1symfony-1.4traits

Using traits in Symfony 1.4


I've tried using Traits for some models in Symfony 1.4. The trait file is located in lib/model/MyTrait and is as follows:

<?php 
trait MyTrait {
   // some code
}

And I'm using it inside a model as

<?php
class ModelPeer {
    use MyTrait;
    // some code
}

But they don't seem to work as I get the following error.

Fatal error: Trait 'MyTrait' not found in /lib/model/MyTrait.php

Solution

  • I strongly suggest you to read this post.

    https://capocasa.net/trait-with-symfony-1-4

    Your problem seems to be of php versions. Specifically, the versions in which symfony 1.4 was develop. Since it includes no support for traits.

    As it explains, in order for the autoloader to load your trait, you must subclass the symfony sfAutoloadConfigHandler and add traits to the used regexp.

    As a not so pretty work around I suggest you to define something before the trait. That way you can be sure to have it on scope when running your symfony 1.4 code, like this:

    <?php
    class UtterlyUseless {
    } 
    trait MyTrait {
       // some code
    }