簡體   English   中英

在一個函數中分配類屬性,並在php中的另一個函數中使用它

[英]Assign class property in one function and use it in another function in php

請考慮此類,我有一個包含兩種形式的html頁面 ,首先我提交form1 ,然后再提交第二種。 我在第一個函數中更改了$aaa值,但是在提交第二個表單后,它顯示了空數組,並且在func1丟失了分配的值。
我想訪問func1在指定的值func2

class Myclass
{
    private $aaa = array();

    public function func1() {
        $this->aaa= [1, 2, 3];
    }

    public function func2() {
        var_dump($this->aaa);
    }
}

在HTML文件中:

<form action=" FUNC1 " method="post" id="form1">
    <button type="submit">FUNC1</button>
</form>

<form action=" FUNC2 " method="post" id="form2">
    <button type="submit">FUNC2</button>
</form>
class MyClass
{

public function __construct(
    $closure
) {
    $this->aaa = $closure();
}

public function func1() ....

現在,您可以將數據保存在該用戶瀏覽器的單個會話中

session_start();
$myClass = new MyClass(function () use($_SESSION) { if(isset($_SESSION['index_here'])) { return $_SESSION['index_here']; } });

然后在添加或更改對象屬性aaa時相同-更新會話

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM