Jump to content

How can I call a property of the class?


lopes_andre

Recommended Posts

Hi,

 

I'm giving my first steps with OOP PHP.

 

I need do pass a property in a object call but I can't do it.

 

Here is the code:

 

<?php


$testar_classe = new geoIpImportCSV('geolitecity', 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/');
echo $testar_classe->downloadAndSaveFile('./', $testar_classe->obtainDownloadFileName()); 
echo $testar_classe->uncompressZipFile($testar_classe->toDownloadFileName, './'); 
//echo $testar_classe->deleteLine(1, 'GeoLiteCity-Location.csv');
//echo $testar_classe->deleteLine(1, 'GeoLiteCity-Blocks.csv'); 
//echo $testar_classe->insertLinesToDb('GeoLiteCity-Location.csv', 'tabela1'); 
//echo $testar_classe->insertLinesToDb('GeoLiteCity-Blocks.csv', 'tabela2'); 



class geoIpImportCSV {


// Propriedades Privadas
private $input_zip_file;   
private $url_without_file; 
private $input_csv_file;   
private $csvType;          
private $toSaveDir;		   

// Propriedades Públicas
public  $toDownloadFileName; 


function __construct($csvType, $url_without_file) {
	$this->url_without_file  = $url_without_file;
	$this->csvType           = $csvType;
}

//.. more code here ...

/*
Este método faz o download e salva o ficheiro em questão no disco.
*/
public function downloadAndSaveFile($toSaveDir, $toDownloadFilename) {

	// Vou assignar a $toSaveDir como variavel de classe
	$this->toSaveDir = $toSaveDir;

	// Vou saber qual o URL do ficheiro a fazer download
	$urlFilenameToDownload = $this->url_without_file . $toDownloadFilename;
	//$urlFilenameToDownload = "http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz"; // Para efeitos de DEBUG, COMENTAR/APAGAR

	// Vou fazer o download do ficheiro e gravar p o disco
	if (isset($urlFilenameToDownload) == true){
		$ToDir = $toSaveDir; ## Where the files will be made on your server.
		$FileContents = implode('', file($urlFilenameToDownload));
		$FileNameP = explode('/', $urlFilenameToDownload);
		$FileName = $FileNameP[count($FileNameP) - 1];

		if (($FileContents != '') && (fopen($ToDir . $FileName, 'w') != 0)){
			$File = fopen($ToDir . $FileName, 'w');
			$Write = fwrite($File, $FileContents);
				if ($Write != 0){
					echo 'The file ' . $ToDir . $FileName . ' was successfully created!';
					fclose($File);
				}
				else{
					echo 'There was an error; the file could not be created.';
				}
		}
	}		
}

/*
Este método descomprime ficheiros zip e coloca-os numa directoria.
*/
public function uncompressZipFile ($fileNameToUncompress, $extractTo) {

	// Criar a instancia	
	$zip             = new ZipArchive;
	// Abrir
	$open_file       = $zip->open($fileNameToUncompress);
	// Extrair para
	$extract_file_to = $zip->extractTo($extractTo);
	// Fechar
	$close_zip       = $zip->close();

	// Ver se correu tudo bem
	if(!$close_zip) {
		die("Error: the uncompress Zip has not have complete correctly."); 
	} else {
		return "ok";
	}
}

/.. more code here ..

}


?>

 

What I'm trying to achieve is to call the property "$toDownloadFileName" in the call to the object "echo $testar_classe->uncompressZipFile($testar_classe->toDownloadFileName, './');" but gives me error.

 

If I call in this way, it works:

"echo $testar_classe->uncompressZipFile(/*$testar_classe->toDownloadFileName*/ 'GeoLiteCity_20101201.zip', './');"

 

How can I call the property of the class in the object method?

 

Best Regards,

 

Link to comment
https://forums.phpfreaks.com/topic/222301-how-can-i-call-a-property-of-the-class/
Share on other sites

You don't need to include it in a call to the method. Since it is previously defined you can call it IN the method using $this->toDownloadFileName

 

Example:

 

The Call

[

echo $testar_classe->uncompressZipFile('GeoLiteCity_20101201.zip', './');

 

The method

public function uncompressZipFile ($fileNameToUncompress, $extractTo)
{
    // Criar a instancia
    $zip             = new ZipArchive;

    // Abrir
    $open_file       = $this->toDownloadFileName . $zip->open($fileNameToUncompress);

    // Extrair para
    $extract_file_to = $zip->extractTo($extractTo);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.