我对类方法有一个非常奇怪的问题:
如果我用
打电话 $allegato->delete($fileDaEliminare)
(案例1)
我无法访问我的命名常量...但是如果我调用相同的方法
function delete($fileDaEliminare = null) {
if ($fileDaEliminare == "tutti") {
$query = "DELETE FROM " . $this->table_name . " WHERE idIncarico = :idIncarico";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':idIncarico', $this->idIncarico);
$stmt->execute();
*** CASE 1 : ERROR ***
$percorso = APPROOT . DS . UPLOADFOLDER . DS . $this->idIncarico . "*";
$this->fileSystem->eliminaCartella($percorso);
} else {
$query = "DELETE FROM " . $this->table_name . " WHERE fileName = :fileName and idIncarico = :idIncarico";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':fileName', $fileDaEliminare);
$stmt->bindParam(':idIncarico', $this->idIncarico);
$stmt->execute();
*** CASE 2 : OK ***
$percorso = APPROOT . DS . UPLOADFOLDER . DS . $this->idIncarico;
$nomeFile = $fileDaEliminare;
$this->fileSystem->eliminaFile($percorso . DS . $nomeFile);
// elimina cartella se vuota
if ($this->fileSystem->cartellaVuota($percorso)) {
$this->fileSystem->eliminaCartella($percorso);
}
}
}
(案例2)
一切都按预期工作。
这是类方法的完整代码:
get_defined_constants
使用[user]
在两种情况下转储已定义的常量,{2}确实缺少define('TIMEZONE', 'Europe/Rome');
define('UPLOADFOLDER', "uploads");
define('PERIZIEFOLDER', "perizieGenerate");
define('DS', DIRECTORY_SEPARATOR);
define('APPROOT', realpath(__DIR__ . '/..'));
部分...
的config.php
@IBOutlet weak var navigationBar: UINavigationBar!
//playToPause()
@IBAction func playButton(sender: UIBarButtonItem) {
let newBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "pauseButton:")
navigationBar.topItem?.rightBarButtonItem = newBarButton
}
// pauseToPlay()
@IBAction func pauseButton(sender: UIBarButtonItem){
let pauseBtnItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playButton:")
navigationBar.topItem!.rightBarButtonItem = pauseBtnItem
}
答案 0 :(得分:1)
这是一个基于您的模糊代码示例的示例,不知道它是否会解决您的特定问题,因为您没有提供更多信息,但这里是:
<?php
// prepare test vars and constants
define('DS', DIRECTORY_SEPARATOR);
define('UPLOADFOLDER', 'UPLOADFOLDERconstant');
define('APPROOT', '/var/www/approotDir');
class MyClass{
var $idIncarico = 999;
function delete($fileDaEliminare = null) {
if ($fileDaEliminare == "tutti") {
echo 'CASE 1: ', $fileDaEliminare, "<br>";
$percorso = UPLOADFOLDER;
echo '$percorso: ', $percorso, "<br><br>";
}
else {
echo 'CASE 2: ', $fileDaEliminare, "<br>";
$percorso = APPROOT . DS . UPLOADFOLDER . DS . $this->idIncarico;
echo '$percorso: ', $percorso, "<br><br>";
}
}
}
$fileDaEliminare = 'test123';
$allegato = new MyClass();
$allegato->delete('tutti'); // test CASE 1
$allegato->delete($fileDaEliminare); // test CASE 2
?>
PS 您原始问题的评论中包含有效点,您的错误表明问题是 UPLOADFOLDER 常量,因此请查看其中的位置被定义为。请注意,在上面的示例代码中,我在顶部定义了相同的常量。