Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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-在整个类中更改属性值_Php_Wordpress_Properties - Fatal编程技术网

PHP-在整个类中更改属性值

PHP-在整个类中更改属性值,php,wordpress,properties,Php,Wordpress,Properties,我的类中有两个函数,需要使用其中一个函数的信息在另一个函数中做出决策。我认为我可以通过将属性设置为新值来改变属性的值,就像它在Javascript函数中工作一样,但这是一个很大的误解。如何在整个类中更改属性的值 class Show_Or_Not { public $num; public function __construct() { add_action( 'woocommerce_cart_calculate_fees', array( $this, 'c

我的类中有两个函数,需要使用其中一个函数的信息在另一个函数中做出决策。我认为我可以通过将属性设置为新值来改变属性的值,就像它在Javascript函数中工作一样,但这是一个很大的误解。如何在整个类中更改属性的值

class Show_Or_Not {

    public $num;

    public function __construct() {

     add_action( 'woocommerce_cart_calculate_fees', array( $this, 'check_cart_for_condition'), 50 );
     add_filter( 'wc_add_to_cart_message', array( $this, 'use_the_cart_condition'), 100, 2 );

    }

    public function check_cart_for_condition() {

      // Ton of code checking how often a certain category occurs in the cart.

      if ( $cat_in_cart == 1 ) {
          // Trying to update value of class property in 
          // order to use it in the next function.
          $this->num = 1;
      } elseif ( $cat_in_cart == 2 ) {
          // Trying to update value of class property in 
          // order to use it in the next function.
          $this->num = 2;
      }

    }

    public function use_the_cart_condition() {

      // If condition determined in upper function is met. 
      if ( $this->num == 1 ) {
         // Do something
      } elseif ( $this->num == 2 ) {
         // Do something
      }

    }

}

$newClass = new Show_Or_Not();

调用操作或筛选器时,第二个值需要是可以在主题的functions.php文件中找到的方法的名称。您可以向文件中添加自定义方法

// In the functions.php file for the theme

function check_cart_for_condition() {
     // get the session
     global $session;

    // initialize the $num var
    $num = 0;
    // Ton of code checking how often a certain category occurs in the cart.
    if ( $cat_in_cart == 1 ) {
        // Trying to update value of class property in 
        // order to use it in the next function.
        $num = 1;
     } elseif ( $cat_in_cart == 2 ) {
        // Trying to update value of class property in 
        // order to use it in the next function.
        $num = 2;
    }

    // This only needs to be for the next request since the hooks
    // run back to back, add it to the session flash data
    $session->set_flashdata( 'num', $num );
}


public function use_the_cart_condition() {
     global $session;
     // Retrieve Flashdata
     $num = $session->flashdata( 'num' );

    // If condition determined in upper function is met. 
    if ( $this->num == 1 ) {
       // Do something
    } elseif ( $this->num == 2 ) {
       // Do something
    }
}
现在,只需将其添加到您需要的代码中:

 add_action( 'woocommerce_cart_calculate_fees','check_cart_for_condition', 50 );
 add_filter( 'wc_add_to_cart_message','use_the_cart_condition', 100, 2 );

显示类定义是一个很好的开始,但您是否也可以发布一些示例代码,说明您希望如何实例化和使用该类?似乎您必须在
set\u the_condition()
中传递一个参数,类似于
set\u the_condition($condition)
您的其余代码在哪里?你如何调用这些方法?@Ja͢ck我已经更新了上下文。关键是我不能在同一个函数中同时设置和使用条件(使用WordPress操作和过滤器),这就是为什么我认为它可以使用类属性来实现这一点。我的问题更多的是PHP而不是WordPress,只是指出我正在尝试更新一个属性以进一步使用它;如何,在
内部使用购物车条件()
您总是调用
$this->检查购物车条件()?谢谢你的回复,很抱歉我错过了。我现在将提供更多关于这个问题的背景。并不是说我遇到了致命的错误。我冒昧地整理了你答案的第一部分;-)谢谢你,杰克。我曾考虑将其设置为一个部分,但我想我会尽量详细。好吧,那么,我将修改我的答案,但我认为您试图在类中实现的目标是不可能的。我可能错了,但我会在回答中解释。谢谢!这是一种行之有效的方法。WordPress不支持$session,但WooCommerce确实能够使用WC()->session->set/get设置和获取会话数据。