简体   繁体   中英

How to access the class using include/require using php?

I set up all the basic setup for Codeigniter Framework.

My Code:

controllers/index.php:

include "file_name.php"; //In my case APPPATH . "controllers/user/user_data.php"

controllers/user/file_name.php: //In my case controllers/user/user_data.php

<?php 

    echo "Welcome";

    class File_name 
    {
        function index()
        {
            echo "this is index";
            # code...
        }
    }

Output: What I getting:

welcome

What I need:

welcome this is index

My problem is I have file_path instead of file_name in include, so I am unable to create Object for the file.?

echo "Welcome ";
$fileName = new File_name();
$fileName->index(); 

In your index.php file:

include "file_name.php";
$file_name_obj = new File_Name();
$file_name_obj->index();

In your file_name.php file change:

function index()

to:

public function index ()

if your trying to include a file, be sure to remember that your using a framework, CI. the proper way of including a file using codeigniter is using the "$this->load->view('name of file');" method, because , there are instance that using the include cause an error because it use preferably for pure php style not framework style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM