簡體   English   中英

如何使用php使用include / require訪問類?

[英]How to access the class using include/require using php?

我為Codeigniter Framework設置了所有基本設置。

我的代碼:

controllers / index.php:

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

controllers / user / file_name.php://就我而言,controllers / user / user_data.php

<?php 

    echo "Welcome";

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

輸出:我得到的是:

welcome

我需要的:

welcome this is index

我的問題是在include中有file_path而不是file_name,所以我無法為該文件創建對象。

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

在您的index.php文件中:

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

在您的file_name.php文件中進行更改:

function index()

至:

public function index ()

如果您嘗試包含文件,請務必記住您使用的是CI框架。 使用codeigniter包含文件的正確方法是使用“ $ this-> load-> view('文件名');” 方法,因為,存在使用include導致錯誤的實例,因為它最好用於純PHP樣式而不是框架樣式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM