Jump to content
php.lv forumi

CI objektu glabāšana sessijā


entu

Recommended Posts

Briedi, pats izlasīji? Es izlasīju visu, kas nav komentāri.

Visam būtu jāstrādā, bet problēma - nestrādā kā vajadzētu.

 

Uztaisi divus failus: foo1.php, foo2.php.

Abos iemet "session_start()". Pirmajā uztaisi klasi ar divām metodēm (get_x(), set_x()) un mainīgo x.

Tajā pašā pirmajā failā izsauc objektu, piešķir to mainīgajam foo. Tad ar metodi "uz'set'o" objekta mainīgo x.

Uztaisi serialize() un jau serializēto mainīgo iemet sesijā.

Otrā failā paņem attiecīgo sesijas mainīgo un uztaisi var_dump(). Es neredzēju attiecīgās metodes. Tikai mainīgo x.

Link to comment
Share on other sites

Es neredzēju attiecīgās metodes. Tikai mainīgo x.

 

..nu bet kā tu iztēlojies, ka tur būs pats metožu kods?

Jā, kā jau minēji, attiecīgajai klasei jābūt iekļautai.

 

Šis man strādā:

 

foo1.php

<?
session_start();
include 'FooBar.php';

$foobar = new FooBar;
$foobar->setX(5);
$_SESSION['foobar'] = serialize($foobar);


var_dump($_SESSION['foobar']);
var_dump($foobar);
echo $foobar->getX();

 

foo2.php

<?
session_start();
include 'FooBar.php';

$foobar = unserialize($_SESSION['foobar']);

var_dump($_SESSION['foobar']);
var_dump($foobar);
echo $foobar->getX();

 

FooBar.php

<?
class FooBar{
private $x;

public function setX($x){
	$this->x = $x;
}

public function getX(){
	return $this->x;
}
}

Link to comment
Share on other sites

Vai tad šis jau visu nepasaka?

Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable interface or the magic __sleep and __wakeup methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized.
Link to comment
Share on other sites

Vai tad šis jau visu nepasaka?

 

If an internal class does not fulfill any of those requirements, it cannot reliably be serialized.

 

Nu redzi, man veiksmīgi izdevās serializēt gan bez tā interfeisa, gan bez maģiskajām metodēm.

Link to comment
Share on other sites

Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable interface or the magic __sleep and __wakeup methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...