Php 类中的变量在方法中不可访问

Php 类中的变量在方法中不可访问,php,laravel,Php,Laravel,我在一个类的函数方法外声明了一个变量。 我通过发送ajax请求更改了值。这个要求很好。 但是当我回显变量名时,它仍然返回null,并且根本没有改变。我怎样才能正确地改变它 这是我的密码 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use DB; use App\Helpers\globalFunction; class register

我在一个类的函数方法外声明了一个变量。 我通过发送ajax请求更改了值。这个要求很好。 但是当我回显变量名时,它仍然返回null,并且根本没有改变。我怎样才能正确地改变它

这是我的密码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use DB;
use App\Helpers\globalFunction;

class registerController extends Controller
{
    public $documentType;
    public $trackingNumber;
    public $recipient;
    public $description;
    public $myvalue = 'hello';

    public function pendingDataBeforeSave(Request $request)
    {
        $this->documentType = $request->documentType;
        $this->trackingNumber = $request->trackingNumber;
        $this->recipient = $request->recipient;
        $this->description = $request->description;
        $this->myvalue = 'new one';
    }

    public function storeSend(Request $request)
    {
        $office = $request->office;
        $sendTo = $request->sendTo;
        $status = $request->status;
        $message = $request->message;
        echo json_encode($this->documentType);
    }
}
这是pendingDataBeforeSave函数的客户端代码

var xhr = new XMLHttpRequest();

var object = {
    documentType: documentType.value,
    trackingNumber: trackingNumber.value,
    recipient: recipient.value,
    description: description.value
};

xhr.open("POST", "/pendingDataBeforeSave");
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("X-CSRF-TOKEN", document.head.querySelector("[name='csrf-token']").content);
xhr.send(JSON.stringify(object));
xhr.onload = function () {
    if (xhr.status == 200) {
        alert('sent');
    } else {
        alert(xhr.responseText);
    }
};

不需要整个类,因为在
Route::post('/pendingDataBeforeSave','registerController@pendingDataBeforeSave“
”,在下列情况下不会被保留,
Route::post(“/sendData”,”registerController@storeSend“);
正在被调用


除非有一种机制,在这种机制中,值存储在不同的请求中,这里没有提到过这种机制?

从它的角度来看,这两种方法是通过两种不同的路径处理的。PHP的工作方式是,一旦第一个请求完成,它就不会在多个请求中保留属性值。

可以通过ajax请求发布整个类?如何调用
storeSend
pendingDataBeforeSave
方法?我从客户端发送请求,路由是route::post(“/pendingDataBeforeSave”,”registerController@pendingDataBeforeSave和路由::post('/sendData','registerController@storeSend');如果你展示这样的小片段,我们无法知道你是如何编写这个类的。因此,请记住,我编辑了我的代码。我发布了整个类。请看一些更改。它不会保存我从第一个请求中存储的数据。你不是在保存数据,而是在设置值,然后丢弃Ajax请求末尾的类(当数据反馈给客户端时)。然后用第二个Ajax请求创建一个新类。否。PHP中内置的多个请求之间没有持久性。感谢您向我说明这一点。我已经面对它6个小时了。问题是类的状态及其内容不能跨两个不同的Ajax调用存储,它们是无状态的。基本上,它们创建了一个副本因此,第二次调用中的变量没有定义。这可以通过向类添加构造函数并将变量设置为值来证明。相同的值将在第二次Ajax请求中通过。也许您需要将信息存储在会话变量中,以便不同的用户访问它租用Ajax请求?我怎么做?你有示例代码要构造吗?我已经尝试过了,但出现了错误。这是我的代码。公共函数uu构造($documentType,$trackingNumber){$this->documentType=$documentType;$this->trackingNumber=$trackingNumber;}是的,我确实是指会话。:)非常感谢。感谢您让我更清楚地了解客户机和服务器。
var xhr = new XMLHttpRequest();

var object = {
    documentType: documentType.value,
    trackingNumber: trackingNumber.value,
    recipient: recipient.value,
    description: description.value
};

xhr.open("POST", "/pendingDataBeforeSave");
xhr.setRequestHeader("Content-type", "text/plain");
xhr.setRequestHeader("X-CSRF-TOKEN", document.head.querySelector("[name='csrf-token']").content);
xhr.send(JSON.stringify(object));
xhr.onload = function () {
    if (xhr.status == 200) {
        alert('sent');
    } else {
        alert(xhr.responseText);
    }
};