【问题】
折腾:
期间,调试了半天,发现加了构造函数后:class crifanLib {
private $logFile;
function __construct() {
printAutoNewline("into crifanLib __construct");
echo "into __construct 222";
//logInit();
}
/***********************************************************************************************
* String/Path
***********************************************************************************************/
/*
* 【已解决】PHP中如何实现路径拼接(两个路径合并)以及合并文件夹路径和文件名
* https://www.crifan.com/php_path_concatenation_combine_directory_and_filename
* eg:
* from:
* D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\php_test\35934503_data
* cookie_jar.txt
* to:
* D:\tmp\WordPress\DevRoot\httpd-2.2.19-win64\httpd-2.2-x64\htdocs\php_test\35934503_data\cookie_jar.txt
*/
function concatenatePath($headPath, $tailPath) {
$realHeadPath = realpath($headPath);
// printAutoNewline("realHeadPath=".$realHeadPath);
//$realTailPath = realpath($tailPath);
//printAutoNewline("realTailPath=".$realTailPath);
//$concatnatedPath = $realHeadPath.DIRECTORY_SEPARATOR.$realTailPath;
// printAutoNewline("tailPath=".$tailPath);
$concatnatedPath = $realHeadPath.DIRECTORY_SEPARATOR.$tailPath;
// printAutoNewline("concatnatedPath=".$concatnatedPath);
return $concatnatedPath;
}
/***********************************************************************************************
* Log
***********************************************************************************************/
/*
Init log file
*/
function logInit($inputLogFile = null) {
// set default log file name, path is current php file
// // in case of Windows set default log file
// //http://stackoverflow.com/questions/1482260/how-to-get-the-os-on-which-php-is-running
// //http://php.net/manual/zh/function.php-uname.php
// if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// $defautLogFile = 'C:/php/defLogFile.log';
// }
// // set default log file for Linux and other systems
// else {
// $defautLogFile = '/tmp/defLogFile.log';
// }
printAutoNewline("Before call getcwd()");
echo "Before call getcwd() 222";
$logFolder = getcwd();
printAutoNewline($logFolder);
$logFilename = "log_".date('YmdHis').".log";
printAutoNewline($logFilename);
$defautLogFile = concatenatePath($logFolder, $logFilename);
printAutoNewline($defautLogFile);
$this->logFile = $inputLogFile ? $inputLogFile : $defautLogFile;
printAutoNewline($this->logFile);
}
别处调用:include_once "crifanLib.php";
//test log
$crifanLib = new crifanLib();
//$crifanLib->logInit("/var/www/120.26.121.239/public_html/php/access_token/logTest.log");
$crifanLib->logWrite("This is crifanLib log test message not pass log file name");
代码不工作了:
没有任何输出。
【折腾过程】
1.后来是加了:include_once "crifanLib.php";
//test log
print_r("before into crifanLib 111 \r\n");
echo "before into crifanLib 222\r\n";
$crifanLib = new crifanLib();
//$crifanLib->logInit("/var/www/xxxx/public_html/php/access_token/logTest.log");
$crifanLib->logWrite("This is crifanLib log test message not pass log file name");
才看到输出:
2.然后验证了推断:
应该是由于__construct有问题,不工作。
搜:
php __construct not work
参考:
提到了php v4是旧的语法。
所以去看了看此处自己的PHP的版本,的确是v5的:root@chantyou:access_token# php --version
PHP 5.4.16 (cli) (built: Jun 23 2015 21:17:27)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
所以不存在该问题。
3.还是不知道错误出在何处。
又去调试了半天,终于知道错误原因了:
搜:
php __construct call other function
参考:
才知道如何改:
前面要加$this->yourFunction();
即,之前的:class crifanLib {
private $logFile;
function __construct() {
logInit();
}
/*
Init log file
*/
function logInit($inputLogFile = null) {
......
}
和:class crifanLib {
private $logFile;
function __construct() {
echo "into __construct 111111";
printAutoNewline("into crifanLib __construct 2222222");
}
function printAutoNewline($contentToPrint) {
print_r($contentToPrint."
");
}
都是有问题的:
不是在构造函数中__construct不能调用类中的用户自定义的其他函数;
而是,在类中,调用自己类内部的函数,要用$this->xxx才行。。。
【总结】
PHP中,不论是在类的哪里,包括构造函数__construct中,调用别的东西,包括变量,函数,都要记得写成$this->xxx
$this->xxx()
而不要直接写成xxx
xxx()
了。否则就错误了,并且没有任何报错输出。。。。