dongtun3328 2018-11-13 00:28
浏览 37
已采纳

为什么我不能使用PHP访问类中的函数

An Error occurred when a function is called inside a Class.

The error message:

Parse error: syntax error, unexpected '$arquivo' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in C:\xampp\htdocs\sequencial\function_sintegra.php on line 20

Here I provide my code:

<?php

function explode_txt(){

    $arquivo = $_SESSION['nomeArquivo'];
    $conteudo = file_get_contents($arquivo);
    $linhas = explode("
", $conteudo);
}

class Sintegra{

    private $emissor;
    private $cnpj;
    private $inscricao;
    private $municipio;
    private $estado;
    private $dataInicial;
    private $dataFinal;

    $arquivo = explode_txt(); //Arquivo Sintegra em array nesta variavel, por linhas.

    $emissor = (string)substr($arquivo[0], 30, 35);
    $inscricao = (string)substr($arquivo[0], 16, 14);
    $cnpj = (string)substr($arquivo[0], 2, 14);

}

?>

Is there something wrong with how I access the function? I have tried to browse but could not find any solution. Thank you for your help.

  • 写回答

1条回答 默认 最新

  • dsv17139 2018-11-13 00:34
    关注

    You need to call outside function in class method.

    You can do this way:

    class Sintegra{
    
        private $emissor;
        private $cnpj;
        private $inscricao;
        private $municipio;
        private $estado;
        private $dataInicial;
        private $dataFinal;
    
        public function __construct() {
            $arquivo = explode_txt();
    
            $this->emissor = (string)substr($arquivo[0], 30, 35);
            $this->inscricao = (string)substr($arquivo[0], 16, 14);
            $this->cnpj = (string)substr($arquivo[0], 2, 14);
        }
    }
    

    Update This is the best way to do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统