php 5.3 construct_【已解决】PHP中的构造函数__construct不工作

【问题】

折腾:

期间,调试了半天,发现加了构造函数后: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");

才看到输出:

d9336f2e70fe8ef3f35e2a3fd9d1dfe6.png

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()

了。否则就错误了,并且没有任何报错输出。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值