简体   繁体   中英

Why I can't access a function inside a class, using 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("\n", $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.

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.

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