Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将过程PHP转换为OOP_Php_Wordpress_Oop_Woocommerce - Fatal编程技术网

将过程PHP转换为OOP

将过程PHP转换为OOP,php,wordpress,oop,woocommerce,Php,Wordpress,Oop,Woocommerce,我对OOP非常陌生,但我认为我已经掌握了基础知识,我正在尝试通过实践来学习。因此,我决定将我的简单wordpress插件从过程代码转换为类方法,但我不断遇到致命错误 以下是我的程序代码: <?php if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } //Check if WooCommerce is active if ( is_plugin_active_for_network( 'woo

我对OOP非常陌生,但我认为我已经掌握了基础知识,我正在尝试通过实践来学习。因此,我决定将我的简单wordpress插件从过程代码转换为类方法,但我不断遇到致命错误

以下是我的程序代码:

<?php
if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

//Check if WooCommerce is active
if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {

//Load plugin styles
function woa_wqfsp_stylesheet() 
{
    wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
}
add_action('wp_enqueue_scripts', 'woa_wqfsp_stylesheet');

//add quantity fields
add_filter( 'woocommerce_loop_add_to_cart_link', 'woa_add_quantity_fields', 10, 2 );
function woa_add_quantity_fields( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

}//main end
?>

这是我的OOP代码,但我不断收到错误:

<?php

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}

function woa_wqfsp() {
    return woa_wqfsp();
} // End woa_wqfsp()

woa_wqfsp();

class woa_wqfsp {

    private static $_instance = null;
    private $html;
    private $product;

    public function __construct() {

        add_action( 'init', array( $this, 'setup' ) );
    }

    public static function instance() {
        if ( is_null( self::$_instance ) )
            self::$_instance = new self();
        return self::$_instance;
    }

//Load plugin styles
function woa_wqfsp_stylesheet() 
{
    wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
}



function woa_add_quantity_fields( $html, $product ) {
    if ( $this->$product && $this->$product->is_type( 'simple' ) && $this->$product->is_purchasable() && $this->$product->is_in_stock() && ! $this->$product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $this->$product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $html .= woocommerce_quantity_input( array(), $this->$product, false );
        $html .= '<button type="submit" class="button alt">' . esc_html( $this->$product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}

function woa_wqfsp_run() {
//Check if WooCommerce is active
if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
//add style for quantity field
add_action('wp_enqueue_scripts', $this->woa_wqfsp_stylesheet);
//add quantity fields
add_filter( 'woocommerce_loop_add_to_cart_link', $this->woa_add_quantity_fields, 10, 2 );

}
}

}//class end
?>

有人能指出我做错了什么吗?我认为这是围绕这一部分展开的:函数woa\u add\u quantity\u字段($html,$product)

$html只是一个空变量,而product是woocommerce的一个全局变量


提前感谢

首先:
遵循一些,以大写字母开始类名

其次: 角色

绝对没有意义,可能应该返回类对象

第三个(您可能会遇到错误): 线路

add_action( 'init', array( $this, 'setup' ) ); 
在类缺少的
init
上调用
setup()
方法

如果我们总结一下,你会得到:

function woa_wqfsp() {
    return new Woa_Wqfsp();
} // End woa_wqfsp()

woa_wqfsp();

class Woa_Wqfsp {

    private $html;
    private $product;

    public function __construct() {

        add_action( 'init', array( $this, 'setup' ) );
    }

    public function setup(){
        // do setup
    }

    //Load plugin styles
    function woa_wqfsp_stylesheet()
    {
        wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
    }

    function woa_add_quantity_fields( $html, $product ) {
        if ( $this->$product && $this->$product->is_type( 'simple' ) && $this->$product->is_purchasable() && $this->$product->is_in_stock() && ! $this->$product->is_sold_individually() ) {
            $html = '<form action="' . esc_url( $this->$product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
            $html .= woocommerce_quantity_input( array(), $this->$product, false );
            $html .= '<button type="submit" class="button alt">' . esc_html( $this->$product->add_to_cart_text() ) . '</button>';
            $html .= '</form>';
        }
        return $html;
    }

    function woa_wqfsp_run() {
        //Check if WooCommerce is active
            if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
            //add style for quantity field
            add_action('wp_enqueue_scripts', $this->woa_wqfsp_stylesheet);
            //add quantity fields
            add_filter( 'woocommerce_loop_add_to_cart_link', $this->woa_add_quantity_fields, 10, 2 );

        }
    }

}//class end
函数woa_wqfsp(){
返回新的Woa_Wqfsp();
}//结束woa_wqfsp()
woa_wqfsp();
Woa_Wqfsp类{
私人$html;
私人产品;
公共函数构造(){
添加动作('init',数组('this',setup');
}
公共功能设置(){
//设置
}
//加载插件样式
函数woa_wqfsp_样式表()
{
wp_enqueue_style('wqfspCSS',plugin_dir_path('css/style.css',_u文件_u));
}
函数woa\u添加\u数量\u字段($html,$product){
如果($this->$product&&$this->$product->is_type('simple')&&$this->$product->is_purchable()&&&&$this->$product->is_in_stock()&&!$this->$product->is_selled_selled()){
$html='';
$html.=woocommerce\u quantity\u input(数组(),$this->$product,false);
$html.=''.esc_html($this->$product->add_to_cart_text())。';
$html.='';
}
返回$html;
}
函数woa_wqfsp_run(){
//检查WooCommerce是否处于活动状态
if(网络('woocommerce/woocommerce.php')的插件是否处于活动状态{
//添加数量字段的样式
添加动作('wp\u enqueue\u scripts',$this->woa\u wqfsp\u样式表);
//添加数量字段
添加过滤器('woocommerce\u loop\u add\u to\u cart\u link',$this->woa\u add\u quantity\u字段,10,2);
}
}
}//班尾

另外,考虑到您的问题,您仍然有提高OOP技能的空间。查看SO答案中登记的资源。

您需要说明错误是什么。范围中是否有
add\u action
?我喜欢做的一件事是在
WooCommerce\u loaded
钩子上启动我的WooCommerce扩展。通过这种方式,您应该非常确信所有WooCommerce文件/函数都已加载并可用。但在你告诉我们问题出在哪里之前,我们真的无能为力。首先定义OOP。用
class
关键字将过程代码重写为过程代码是没有意义的。“这是有道理的,只是没用。”Carcigenicate,同意。
function woa_wqfsp() {
    return new Woa_Wqfsp();
} // End woa_wqfsp()

woa_wqfsp();

class Woa_Wqfsp {

    private $html;
    private $product;

    public function __construct() {

        add_action( 'init', array( $this, 'setup' ) );
    }

    public function setup(){
        // do setup
    }

    //Load plugin styles
    function woa_wqfsp_stylesheet()
    {
        wp_enqueue_style( 'wqfspCSS', plugin_dir_path( 'css/style.css', __FILE__ ) );
    }

    function woa_add_quantity_fields( $html, $product ) {
        if ( $this->$product && $this->$product->is_type( 'simple' ) && $this->$product->is_purchasable() && $this->$product->is_in_stock() && ! $this->$product->is_sold_individually() ) {
            $html = '<form action="' . esc_url( $this->$product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
            $html .= woocommerce_quantity_input( array(), $this->$product, false );
            $html .= '<button type="submit" class="button alt">' . esc_html( $this->$product->add_to_cart_text() ) . '</button>';
            $html .= '</form>';
        }
        return $html;
    }

    function woa_wqfsp_run() {
        //Check if WooCommerce is active
            if ( is_plugin_active_for_network( 'woocommerce/woocommerce.php' ) ) {
            //add style for quantity field
            add_action('wp_enqueue_scripts', $this->woa_wqfsp_stylesheet);
            //add quantity fields
            add_filter( 'woocommerce_loop_add_to_cart_link', $this->woa_add_quantity_fields, 10, 2 );

        }
    }

}//class end