/    Sign up×
Community /Pin to ProfileBookmark

Reference an array index, not the value it stores?

So I have an MVC-style framework I’m working on, and I would like to give it the following functionality:

Any number of instances of a model with a certain ID can be created at once, but as soon as a save() method is called on one of these instances, all references to other models with the same ID will now point to the saved one.

The problem comes when I try to track all the references to the different instances of the same model. What I would like to do is store all of the instances in an array, then instead of returning references to the objects themselves when they are pulled, I would return references to the positions in the array. That way, when I save, I can simply go through the array and set each entry to a reference to the saved instance.

As far as I know, however, this is not possible with PHP. Is there any other alternative or perhaps a pattern that is used for cases like this?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Four_StaplesauthorOct 03.2009 — This might help to better illustrate what I want to do:[code=php]<?php
class A {
public static $instances = array();
public $variable = 'original value';

public static function get() {
return self::$instances[] = new A();
}

public function save() {
// needs to set all references to A objects to $this
}
}

$a1 = A::get();
$a2 = A::get();
$a3 = A::get();

$a1->variable = 'updated value';
$a1->save();

echo $a2->variable; // I want this to now be a reference to $a1->variable (ie. output 'updated value')[/code]
Copy linkTweet thisAlerts:
@NogDogOct 03.2009 — Would it make sense to make [b]$variable[/b] static?
Copy linkTweet thisAlerts:
@Four_StaplesauthorOct 04.2009 — Would it make sense to make [b]$variable[/b] static?[/QUOTE]

Unfortunately no, I would like each instance of A to be able to have its own property values, but when one of them is saved ($a1->save()) all other instances of it would take on the saved values ($a2 and $a3 would have the saved values as well).

When I posted the question I hadn't slept in a good 24 hours, so I didn't think of the following solution:[code=php]<?php
class A {
public static $instances = array();
public $variable = 'original value';

public static function get() {
return self::$instances[] = new A();
}

public function save() {
// <do some stuff to update DB>
$this->sync();
}

public function sync() {
foreach(self::$instances as $instance) {
if($instance !== $this) {
$instance->variable = $this->variable;
}
}
}
}

$a1 = A::get();
$a2 = A::get();
$a3 = A::get();

$a1->variable = 'updated value';
$a1->save();

echo $a2->variable;[/code]
So nevermind! ?
×

Success!

Help @Four_Staples spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...