简体   繁体   中英

call a variable in a static function to another static function from same class in php

I have a class with a couple of static functions. One of my functions build a variable and I want to use that variable in another static function.

How can I call that variable?

class MyClass{
    public static function show_preprice_value_column( $column, $post_id ) {
        if ( $column == 'product_preprice' ) {
            $product_preprice = get_post_meta( $post_id, 'product_preprice', true );
            if ( intval( $product_preprice ) > 0 ) {
                echo $product_preprice;
            }
        }
    }

    public static function show_off_value_column( $column, $post_id ) {
        if ( $column == 'product_off' ) {
            var_dump((int)self::show_preprice_value_column());
        }
    }
}

Do you mean this?

<?php
class MyClass
{

    private static $var;

    public static function funcA()
    {
        self::$var = "a";
    }

    public static function funcB()
    {
        self::$var = "b";
    }

}

I used this code :

class Test {
    public static function test1(){
        return 12;
    }

    public static function test2(){
        $var = self::test1();
        echo $var;
        echo "\n".gettype($var);
    }
} 

Test::test2();

And get this as result:

12
integer

So you need to use return after your echo to communicate the value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM