kboyd,
@kboyd@phpc.social avatar

Weird PHP question:

Can an object replace itself in memory with a cast of itself to a sub class?

$object is FreezableArray. I invoke ->freeze(). Can it swap itself out with an instance of FrozenArray (inherits from Freezable)?

I'm guessing the answer is no, but I'd be very intrigued if it wasn't.

Aedius,
@Aedius@lavraievie.social avatar

@kboyd

I managed to do that with serialize / regex / deserialize ...

It worked, it wasn't that slow ... and the surrounding was more curse than this little trick xD

Crell,
@Crell@phpc.social avatar

@kboyd I'm pretty sure you can't, and probably shouldn't. Instead, you can have freeze() return a new instance that is now frozen (whatever that means in context), and overwrite $object with it.

I think that's as close as you can get.

heiglandreas,
@heiglandreas@phpc.social avatar

@Crell @kboyd Otherwise your object would need to know about context outside which would be... Interesting.

But what about a Freezer object that handles all the objects? I mean. Real life analogy would be to prepare something, put it into the freezer and when I need it, I take the frozen thing from there... 🤷

kboyd,
@kboyd@phpc.social avatar

@Crell yeah, that's what I was thinking ... mulling over possible improvements to my "captain cold" library for freezable php arrays

alessandrolai,
@alessandrolai@phpc.social avatar

@kboyd IIRC how Doctrine's proxies work, you can make it work with the opposite: you instatiate the child, but switch to the parent's behavior transparently on demand.

Yeah, that's not what you asked, but if even @ocramius couldn't do any dark magic to do what you're asking, it's probably impossible 😂

Otherwise, you can look at what's Symfony is doing: https://symfony.com/blog/revisiting-lazy-loading-proxies-in-php

ocramius,
@ocramius@mastodon.social avatar

@alessandrolai @kboyd in short: not without a PHP extension.

kboyd,
@kboyd@phpc.social avatar

@ocramius @alessandrolai an extension that should never exist. (And if I was going to write an extension for this, it would solve the original use case rather than this esoteric object-swapping stuff)

emd,
@emd@cosocial.ca avatar

@kboyd so, can it?

kboyd,
@kboyd@phpc.social avatar

@emd dunno. Still assuming "no".

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • DreamBathrooms
  • magazineikmin
  • ethstaker
  • InstantRegret
  • tacticalgear
  • rosin
  • love
  • Youngstown
  • slotface
  • ngwrru68w68
  • kavyap
  • cubers
  • thenastyranch
  • mdbf
  • provamag3
  • modclub
  • GTA5RPClips
  • normalnudes
  • khanakhh
  • everett
  • cisconetworking
  • osvaldo12
  • anitta
  • Leos
  • Durango
  • tester
  • megavids
  • JUstTest
  • All magazines