0

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());
        }
    }
}
2
  • 1
    Which variable are you trying to use where? Commented May 3, 2018 at 14:27
  • @AndrewSchultz I want to use $product_preprice in show_off_value_column function Commented May 3, 2018 at 14:30

2 Answers 2

2

Do you mean this?

<?php
class MyClass
{

    private static $var;

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

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

I want to use a variable in funcB()
Add: private static $product_preprice; to the top of your class and call the variable like so: self::$product_preprice
You can't transfer a variable declared in funcA() to funcB(), but you can use class members to share the variable, like I just did above.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.