Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Just where and how to use a common class available on other projects
  • What I would like is to build a common class of functions that I can use on other projects. Believe me I have tried but nothing seems to work except an include/require. # .\project\classes]controller\project_001.php
    <code><b>
    <?php # Here are a few "uses" that I tried but none work?????
    #use \cq;
    #use model_cq;
    #use \model/cq; #this works fine
    require_once( APPPATH .'classes/model/cq.php'); # common library
    # called in APPPATH .'view\project_001\index.php'
    # usage: cq::set_moving_close_doors(); class Controller_Project_001 extends Controller
    {
    ..
    ..
    ..
    </b></code>
    http://fuel.johns-jokes.com/
    .
  • An application model class like app/classes/model/name.php either lives in the global namespace and is called "Model_name", or lives in the "\Model" namespace and is called "name". You either access it using \Model_Name (case one) or \Model\Name (case two). So it's
    use \Model_Name; // class names are ucfirst() !
    use \Model\Name;  // namespaces require backslashes !
    
  • Harro Verton wrote on Tuesday 26th of June 2012:
    An application model class like app/classes/model/name.php either lives in the global namespace and is called "Model_name", or lives in the "\Model" namespace and is called "name". You either access it using \Model_Name (case one) or \Model\Name (case two). So it's
    use \Model_Name; // class names are ucfirst() !
    use \Model\Name;  // namespaces require backslashes !
    
    Many thanks for taking the time to reply, your solution was very useful.. Further testing has shown these results where it is not essential to extend the Model Class. I am still undecided the best method to adopt but it is always good to have a choice.
    # APPPATH .'classes/controller/lifts.php'
    <?php
    //================================
    class Controller_Lifts extends Controller
    { //====================================
    public function action_index( $data=array() )
    {
    # GOOD
    # ESSENTIAL if not extending Model
    require_once( APPPATH .'classes/model/cqq.php');
    Cqq :: testing_123(); # GOOD
    # LOADED AUTOMATICALLY - DOES NOT REQUIRE a "use" or require statement
    \ Model_Cqq :: testing_123();
    # ALSO GOOD \Model_Cqq :: testing_123();
    # ALSO GOOD Model_Cqq :: testing_123(); # BAD \ Model \ Cqq :: testing_123();
    # Fatal error: Cannot redeclare class Model_Cqq
    # in E:\xamp-lite\...\authors\classes\model\cqq.php
    # on line 321
    die;

    # APPPATH .'classes/controller/model/cqq.php
    <?php
    //===========================
    class Cqq
    { //===========================
    public static function testing_123()
    {
    $a=array
    (
    '__FILE__' => __FILE__,
    '__CLASS__' => __CLASS__,
    '__METHOD__' => __METHOD__,
    '__NAMESPACE__' => __NAMESPACE__,
    '__FUNCTION__' => __FUNCTION__,
    '__LINE__' => __LINE__,
    );
    echo '<dl margin: 1em 4.2em; border:solid 2px">';
    foreach( $a as $key => $value):
    $value = $value ? $value : 'Not declared ???'; echo '<dt><b>' .$key .'</b></dt>';
    echo '<dd>' .$value .'</dd>';
    endforeach;
    echo '</dl>';
    } }#endclass
    //===========================
    class Model_Cqq extends \Model
    { //===========================
    public static function testing_123()
    {
    $a=array
    (
    '__FILE__' => __FILE__,
    '__CLASS__' => __CLASS__,
    '__METHOD__' => __METHOD__,
    '__NAMESPACE__' => __NAMESPACE__,
    '__FUNCTION__' => __FUNCTION__,
    '__LINE__' => __LINE__,
    );
    echo '<dl margin: 1em 4.2em; border:solid 2px">';
    foreach( $a as $key => $value):
    $value = $value ? $value : 'Not declared ???'; echo '<dt><b>' .$key .'</b></dt>';
    echo '<dd>' .$value .'</dd>';
    endforeach;
    echo '</dl>';
    } #}#endclass

    # output

    __FILE__
    E:\xamp-lite\...\authors\classes\model\cqq.php
    __CLASS__
    Cqq
    __METHOD__
    Cqq::testing_123
    __NAMESPACE__
    Not declared ???
    __FUNCTION__
    testing_123
    __LINE__
    30 __FILE__
    E:\xamp-lite\...\authors\classes\model\cqq.php
    __CLASS__
    Model_Cqq
    __METHOD__
    Model_Cqq::testing_123
    __NAMESPACE__
    Not declared ???
    __FUNCTION__
    testing_123
    __LINE__
    59

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion