尽管添加了 __construct(),但我还是收到错误 - "Methods with the same name as their class will not be constructors i



尽管添加了 __construct((,但我还是收到错误 - 已弃用:与类同名的方法在未来版本的 PHP 中将不再是构造函数 它是一个WordPress插件[https://wordpress.org/plugins/datetime-now-button/]代码 -

<?php
/*
Author: radiok
Plugin Name: Date/Time Now Button
Author URI: http://radiok.info/
Plugin URI: http://radiok.info/blog/category/datetime-now-button/
Description: Adds a Now button to the right of date and time fields.
Version: 0.2.2
Text Domain: datetime-now-button
Domain Path: /languages
*/
if ( !class_exists( 'DateTimeNowButtonPlugin' ) ) {
class DateTimeNowButtonPlugin {
function DateTimeNowButtonPlugin() {
add_action( 'init', array( $this, 'InitI18n' ), 10, 1 );
add_action( 'admin_head', array($this, 'AddNowButton' ), 10, 1 );
}
function InitI18n() {
// Place your language file in the languages subfolder and name it "datetime-now-button-{language}.mo" replace {language} with your language value from wp-config.php
load_plugin_textdomain( 'datetime-now-button', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
function AddNowButton() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
if (jQuery('#timestampdiv').length > 0) {
jQuery('#timestampdiv').find('div')
.append('&nbsp;')
.append(jQuery('<a>')
.attr('class', 'now button')
.append('<?php _e( 'Now', 'datetime-now-button' ); ?>')
);
}
if (jQuery('.inline-edit-date').length > 0) {
jQuery('.inline-edit-date').find('div')
.append('&nbsp;')
.append(jQuery('<a>')
.attr('class', 'now button')
.append('<?php _e( 'Now', 'datetime-now-button' ); ?>')
);
}
jQuery('.now.button').bind('click', function() {
<?php
$time_adj = current_time('timestamp');
$cur_mm = gmdate( 'm', $time_adj );
$cur_jj = gmdate( 'd', $time_adj );
$cur_aa = gmdate( 'Y', $time_adj );
$cur_hh = gmdate( 'H', $time_adj );
$cur_mn = gmdate( 'i', $time_adj );
?>
if (jQuery('select[name="mm"]').length > 0) jQuery('select[name="mm"]').val('<?php echo $cur_mm; ?>');
if (jQuery('input[name="jj"]').length > 0) jQuery('input[name="jj"]').val('<?php echo $cur_jj; ?>');
if (jQuery('input[name="aa"]').length > 0) jQuery('input[name="aa"]').val('<?php echo $cur_aa; ?>');
if (jQuery('input[name="hh"]').length > 0) jQuery('input[name="hh"]').val('<?php echo $cur_hh; ?>');
if (jQuery('input[name="mn"]').length > 0) jQuery('input[name="mn"]').val('<?php echo $cur_mn; ?>');
});
});
</script>
<?php
}
}
}
if ( class_exists( 'DateTimeNowButtonPlugin' ) ) $date_time_now_button = new DateTimeNowButtonPlugin();

我尝试将function DateTimeNowButtonPlugin()更改为function __construct()但仍然WordPress显示错误 - 已弃用:与其类同名的方法在未来版本的PHP中将不再是构造函数。还需要做什么更正?我不想隐藏错误消息,我想更正此代码。这还有什么可能导致该错误消息?

您将方法名称更改为__construct()是正确的。尝试像public function __construct()一样声明方法可见性(其他方法也是如此(

<?php
/*
Author: radiok
Plugin Name: Date/Time Now Button
Author URI: http://radiok.info/
Plugin URI: http://radiok.info/blog/category/datetime-now-button/
Description: Adds a Now button to the right of date and time fields.
Version: 0.2.2
Text Domain: datetime-now-button
Domain Path: /languages
*/
if ( !class_exists( 'DateTimeNowButtonPlugin' ) ) {
class DateTimeNowButtonPlugin {
public function run() {
add_action( 'init', array( $this, 'InitI18n' ), 10, 1 );
add_action( 'admin_head', array($this, 'AddNowButton' ), 10, 1 );
}
public function InitI18n() {
// Place your language file in the languages subfolder and name it "datetime-now-button-{language}.mo" replace {language} with your language value from wp-config.php
load_plugin_textdomain( 'datetime-now-button', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
public function AddNowButton() {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
if (jQuery('#timestampdiv').length > 0) {
jQuery('#timestampdiv').find('div')
.append('&nbsp;')
.append(jQuery('<a>')
.attr('class', 'now button')
.append('<?php _e( 'Now', 'datetime-now-button' ); ?>')
);
}
if (jQuery('.inline-edit-date').length > 0) {
jQuery('.inline-edit-date').find('div')
.append('&nbsp;')
.append(jQuery('<a>')
.attr('class', 'now button')
.append('<?php _e( 'Now', 'datetime-now-button' ); ?>')
);
}
jQuery('.now.button').bind('click', function() {
<?php
$time_adj = current_time('timestamp');
$cur_mm = gmdate( 'm', $time_adj );
$cur_jj = gmdate( 'd', $time_adj );
$cur_aa = gmdate( 'Y', $time_adj );
$cur_hh = gmdate( 'H', $time_adj );
$cur_mn = gmdate( 'i', $time_adj );
?>
if (jQuery('select[name="mm"]').length > 0) jQuery('select[name="mm"]').val('<?php echo $cur_mm; ?>');
if (jQuery('input[name="jj"]').length > 0) jQuery('input[name="jj"]').val('<?php echo $cur_jj; ?>');
if (jQuery('input[name="aa"]').length > 0) jQuery('input[name="aa"]').val('<?php echo $cur_aa; ?>');
if (jQuery('input[name="hh"]').length > 0) jQuery('input[name="hh"]').val('<?php echo $cur_hh; ?>');
if (jQuery('input[name="mn"]').length > 0) jQuery('input[name="mn"]').val('<?php echo $cur_mn; ?>');
});
});
</script>
<?php
}
}
}
if ( class_exists( 'DateTimeNowButtonPlugin' ) ) {
$date_time_now_button = new DateTimeNowButtonPlugin();
$date_time_now_button->run();
}

相关内容

最新更新