将静态函数中的变量从 php 中的同一个 class 调用到另一个静态函数
call a variable in a static function to another static function from same class in php
我有一个带有几个静态函数的 class。我的一个函数构建了一个变量,我想在另一个静态函数中使用该变量。
如何调用该变量?
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());
}
}
}
你是这个意思吗?
<?php
class MyClass
{
private static $var;
public static function funcA()
{
self::$var = "a";
}
public static function funcB()
{
self::$var = "b";
}
}
我使用了这段代码:
class Test {
public static function test1(){
return 12;
}
public static function test2(){
$var = self::test1();
echo $var;
echo "\n".gettype($var);
}
}
Test::test2();
得到这个结果:
12
integer
因此您需要在 echo
之后使用 return
来传达价值
我有一个带有几个静态函数的 class。我的一个函数构建了一个变量,我想在另一个静态函数中使用该变量。
如何调用该变量?
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());
}
}
}
你是这个意思吗?
<?php
class MyClass
{
private static $var;
public static function funcA()
{
self::$var = "a";
}
public static function funcB()
{
self::$var = "b";
}
}
我使用了这段代码:
class Test {
public static function test1(){
return 12;
}
public static function test2(){
$var = self::test1();
echo $var;
echo "\n".gettype($var);
}
}
Test::test2();
得到这个结果:
12
integer
因此您需要在 echo
之后使用 return
来传达价值