카테고리 없음

[PHP] Ajax에서 객체를 사용하면 PHP 파일이 호출됩니다.

행복을전해요 2021. 2. 19. 06:13

Use the session to save the object for the next page load.

// Create a new object
$object = new stdClass();
$object->value = 'something';
$object->other_value = 'something else';

// Start the session
session_start();

// Save the object in the user's session
$_SESSION['object'] = $object;

Then in the next page that loads from AJAX

// Start the session saved from last time
session_start();

// Get the object out
$object = $_SESSION['object'];

// Prints "something"
print $object->value;

By using the PHP sessions you can save data across many pages for a certain user. For example, maybe each user has a shopping cart object that contains a list of items they want to buy. Since you are storing that data in THAT USERS session only - each user can have their own shopping cart object that is saved on each page!

-------------------

Another option if you dont want to use sessions is to serialize your object and send it through a $_POST value in your AJAX call. Not the most elegant way to do it, but a good alternative if you don't want to use sessions.

See Object Serialization in the documentation for more informations.

-------------------

mm, you should store in session, $_SESSION["someobj"] = $myobj;, and ensure that when you call the Ajax PHP file this includes the class necessary files which defines the class of $myobj and any contained object in it.

Could you be more specific? I can try.

This is how I create an object then assign it to a session variable:

include(whateverfilethathastheclassorincludeit.php)
$theObject = new TheObjectClass();
//do something with the object or not
$_SESSION['myobject'] = $theObject;

This is how I access the object's members in my Ajax call PHP file:

include(whateverfilethathastheclassorincludeit.php)
$theObject = $_SESSION['myobject'];
//do something with the object
-------------------

If you don't want to move your object that is in your index.php, have your ajax make a request to index.php but add some extra parameters (post/get) that let your index.php know to process it as an ajax request and not return your normal web page html output.

-------------------

You have not provided code, but what I guess is that you need to make your instantiated object global for other scripts to see it, example:

$myobject = new myobject();

Now I want to use this object elsewhere, probably under some function or class, or any place where it is not getting recognized, so I will make this global with the global keyword and it will be available there as well:

global $myobject;

Once you have the object, you can put it into the session and then utilize it in the Ajax script file.

-------------------

다른 사람들이 제안했듯이, $_SESSION그것을 수행하는 표준 방법은 사실, 그것이 해결하기 위해 고안된 세션의 이유 중 하나였습니다. 다른 옵션, 즉 객체 직렬화는 객체를 보유하고 변경되지 않은 상태로 반환하기 위해 클라이언트 측에 의존합니다. 개체의 데이터에 따라 a) 개체에 보안상의 이유로 클라이언트 측에서 사용할 수 없어야하는 정보가 포함될 수 있으며 b) 개체를받은 후 확인해야하기 때문에 좋은 솔루션이 아닙니다.

즉, 클라이언트 측에서 객체를 계속 사용하려는 경우 JSON은 객체 데이터를 직렬화하는 옵션입니다 . PHP의 JSON 함수를 참조하세요 .

-------------------

$ _SESSION에 개체를 저장하는 것과 관련하여 여기에있는 대부분의 답변을 기반으로, 전체 개체가 아닌 AJAX에서 액세스해야하는 개별 속성 만 저장하는 것이 더 효율적입니까, 아니면 중요하지 않습니까?

$_SESSION['object'] = $object;

vs

$_SESSION['property1'] = $object->property1;
$_SESSION['property2'] = $object->property2;

OP가 전체 객체에 액세스하는 것에 대해 묻는 것을 알고 있지만 객체의 특정 속성에만 액세스하고 AJAX에 있으면 객체를 변경하기 위해 클래스의 메서드에 액세스 할 필요가 없는지에 대한 질문이 관련되어 있다고 생각합니다. .



출처
https://stackoverflow.com/questions/22079884