« first day (4374 days earlier)      last day (564 days later) » 

4:12 AM
Hello
I have two table in mysql doctors table and admins table, I use session variables with doctor or admin logining. the doctors should enter some pages only so how to differentiate by sessions variable whose user is logged in?
 
 
6 hours later…
10:31 AM
Any thoughts on github.com/php/php-src/pull/9104 ? (before I write to internals)
 
@ArnaudLeBlanc Sounds sensible to me, but then I'm no expect around that bit :)
 
@ArnaudLeBlanc I found a typo :-) — but I like this idea. It's taken PHP 20+ years to do the stack limiting that Xdebug (naively) implemented :-D
 
11:22 AM
Few days ago I asked if it's possible to get the time in ticks using PHP. Someone suggested stackoverflow.com/a/15028671/1164012. Sadly this isn't very accurate and the ticks I need can not be more than a hour apart from the actual time. Is it possible to get the current time in ticks with PHP?
 
11:43 AM
what are ticks, and what exactly is wrong with a unix timestamp, that can have an arbitrary precision?
 
Sep 29 at 9:32, by cmb
Oh, "A single tick represents one hundred nanoseconds or one ten-millionth of a second.". Never heard of this use of ticks before. Anyway, I don't think there is anything available in PHP; microtime() starts at the Unix epoch, but these ticks start at January 1, 0001 of the proleptic Gregorian calendar. You would need to calculate the difference. https://www.php.net/calendar might help with that.
Apparently.
 
@Danack thanks, Will look into that. And @Sjon; Unfortunately the endpoint I am working with expects a argument in ticks.
 
@Danack aha. Learned something new, thanks
 
I guess I missed cmb's response the last time. :)
 
@Lars have you tried asking whoever that end-point belongs to "how do I correctly generate your ticks?"
 
11:56 AM
@Danack Tried to get in contact, but they haven't responded yet.
 
I was thinking it had something to do with phps declare
 
They've sent a Gist earlier, but they work with C# and C# natively support a tick function
 
...
oh, they just do "getTicks".....and assumed that is everywhere....
 
it's the number of seconds since midnight 0001 UTC, times 10 000 — you should be able to calculate that with PHP, like:
 php -r '$zero = (new DateTime("0001-01-01 00:00:00"))->format("U"); $now = (new DateTime())->format("U"); $ticks = ($now - $zero) * 10000; echo $ticks, "\n";'
that gives an accuracy of seconds
 
12:13 PM
php -r '$zero = (new DateTime("0001-01-01 00:00:00"))->format("U"); $now = microtime(true); $ticks = ($now - $zero) * 10000; echo $ticks, "\n";'

Gives you ~millisconds, which is the best php can do.
 
Morning and Happy Friday All!
 
@Danack Saw that package yesterday, but it basically does the calculation I already tried (github.com/DivineOmega/php-dot-net-ticks/blob/…, $ticks = number_format(($time * 10000000) + 621355968000000000, 0, '.', '');)
 
 
3 hours later…
2:49 PM
Btw @Crell , did I show you this concept? olleharstedt.github.io/programming/2022/03/22/…
 
@OlleHärstedt You had not. The examples are most definitely not PHP, though. :-) Looks more like Javascript written by a Java developer.
 
Hehe
The main example is fetched from the linked Haskell blog post
Oh no, it's C#
I thought I had the Haskell example there too. Someone refactored a piece of "imperative" Haskell into tagless-final bla bla
 
3:10 PM
Writing a PHP interpreter in PHP seems like a lot more work than just writing testable code in the first place.
 
3:21 PM
@Crell Yes, when possible! But the strategy is intended to extend the set of functions that can be written in a pure way/without effects.
A common problem is when logic code depends on result from effectful code. As in the example snippet. And when it's nested.
So strtoupper is a pure function, but is fed content from file, if it exists
 
That's where better code structure helps. Compose functions, not nest classes or function calls.
 
How would you rewrite it? If you're not too busy. The C# snippet, I mean.
/me should find that haskell example too...
 
The strtoupper call should not be in the same function as the file IO.
 
@Crell Yeah, but then you won't be able to test the relationship between IO code and logic code without scaffolding the file?
And if you have to do scaffolding, you can't parallelize your test suit :(
Right, here's the funny Haskell example:

saveFile :: Path -> Bytes -> IO Unit
saveFile p f = do
log ("Saving file" ++ show (name p) ++ " to " ++ show (parentDir p))
r <- httpPost ("cloudfiles.fooservice.com/" ++ (show p)) f
if (httpOK r) then log ("Successfully saved file " ++ show p)
else let msg = "Failed to save file " ++ show p
in log msg *> throwException (error msg)

https://degoes.net/articles/modern-fp
 
3:41 PM
I still can't read Haskell. :-)
 
No one can read that snippet... It's designed to be ugly xD
"Our first step is defining an algebra for the cloud files API." - just Haskell things
 
Here is some lovely byte code:

00002BC0 00 00 00 00 0D D8 ED 5D 98 E3 44 65 17 FC 07 92 .......]..De....
00002BD0 27 0D 18 A9 A5 6E C5 20 97 3C 8C 6E 1E 71 F4 B7 '....n. .<.n.q..
00002BE0 20 D9 91 D2 15 E5 83 9D 6F 2F 96 10 31 7A C5 17 .......o/..1z..
00002BF0 68 BC 02 DB 57 F2 43 E2 AC A3 6E 18 5A 27 DC 05 h...W.C...n.Z'..
00002C00 F3 37 8B DF 0D 3A 97 0B D8 5B D2 65 2A F2 08 5B .7...:...[.e*..[
00002C10 BD 4B 49 E7 B0 01 56 C3 C7 E0 08 73 27 3E E7 6C .KI...V....s'>.l
 
Mm, yes, lovely, huh? :-)
 
yes, very.
 
externals.io/message/118762 - Can someone grant the Wiki karma there (last paragraph of that email)? Or can I do this myself (where?).
 
3:51 PM
I think I can...
 
cmb
@TimWolla done. If you have sufficient karma (not sure where that could be looked up), there is Menu → Admin → User manager
 
oh, i was wondering why he already had it
 
cmb
:)
 
@cmb Within "Admin" I only have "Popularity Feedback" and "Revert Manager", so apparently not. – Thanks!
 
 
6 hours later…
9:36 PM
Here's another architectural principle we don't talk often about in PHP-land:
"Why You Should Defer Side Effects Until the Last Possible Step"
https://spin.atomicobject.com/2018/02/08/defer-side-effects/

Putting side-effects inside an evaluator makes this easy-ish.
Or just a defer() function, why not
 
JRL
the OOP version of this would be a FSM
(Finite State Machine)
 
@JRL You can defer effects with a FSM?
 
JRL
i suppose you could
with a FSM though, the point is that there is no value in defering at all
 
Because...?
 
JRL
"side effects" aren't the boogey man they are in other areas of programming
because a FSM has defined state transitions.
the transitions describe/require all of the information that might be relevant
which means that FSM is a data structure that doesnt work for something like the example necessarily
but for the data structues it does work for, FSM sort of side-steps a lot of the drawbacks or benefits of FP paradigms
because they just aren't necessary
 
9:52 PM
But how do you control the FSM? With a DSL?
 
Usually with methods that transition to some other state.
 
Sounds not easier to unit-test than imperative code?
Would have to see some code regarding effect defer too, to understand that.
But it's late
 
The point of using a state machine is to exhaustively enumerate the possible conditions the system can be in. If the code is unable to represent an illogical state, then you don't need to worry about coding for that illogical state.
 
I gonna explain my point using a pipeline description. Imagine code like:
read-process-write-process-write
If you defer effects, you'll have
read-process-process-write-write
Which means logic and effects will be less entangled with each other. And thus easier to test. :)
 
JRL
I was talking about specifically a Finite state machine
so you can have other kinds of state machines that mostly fit the bill
for instance
the integer type is a state machine
it has the transitions +, -, *, /, **, etc.
each of these transition the state of the integer from a known state to a deterministic state
 
10:03 PM
Sure. Just sounds a bit removed from effect deference. Or I'm stupid x)
 
JRL
no, well
it's not removed, exactly. it's more like... effect deference fixes a problem a state machines don't have
 
I'd have to see an example, I think
 
JRL
suppose you have a user object that has an account status field. the field could be 'registered' or 'verified'. you make that property private so that it cannot be changed by other code, and then make a 'verifyEmail()' method that acts as a state transition for the field.
now the field can only go 'registered' -> 'verified'
 
Now imagine if verifyEmail() must read from file and also check an external email database?
 
JRL
you could then make another method 'updateEmail()' that acts as a state transition for 'verified' -> 'registered'
a file or database that you read without a lock?
in a state transition?
 
10:12 PM
Well, reading from file/db is not a deferable effect, so bad example
 
JRL
no
 
@JRL So where'd you put it?
 
JRL
in the calling code. you would provide as arguments all the data that the state transition needs.
 
Yes, that's why reading is a bad example
 
JRL
generally, state transitions should be pure functions, depending only on the current state of the object it is called on (which should be immutable) and the arguments provided by the caller
 
10:15 PM
Say rather that verifyEmail() should log into database and also send an email as a notification
 
JRL
no
because that's not part of the state transition
that's additional stuff that your application might do obviously
but it's not part of the state transition on the object
you could have the object report the transitions it has gone through since instantiation though
and then have something like if ($user->hasVerifiedTransition())
which would only be true if that transition has been used since the object was first created
 
The user has state "AddressNotVerified". Something happens outside the object, and then $user->emailVerified() is called. The user now transitions to AddressIsVerified, atomically. The emailVerified() method does NOT do DB calls. That's just bad design.

You an also trigger an event "state transitioned from AddressNotVerified to AddressIsVerified", and then send notifications in the event handler. But again, that's separate from the state machine itself.
 
@JRL Meaningless discussion without a use-case, imo, sorry
 
The point is that the user cannot end up in a state of "quasi-verified", and you can make the possible states an enum or similar, so your code cannot even describe "quasi-verified."
Whether the "something happens outside the object" and "event handler" parts do IO is irrelevant to the state machine itself.
 
JRL
i suppose it isn't correct to say that state machines side-step a lot of the FP design. it's more like, state machines are how you design OOP code in a FP way without giving up the advantages you might want from OOP.
 
10:23 PM
OOP hides state transition behind objects. FP eliminates it, except for in the call stack itself.
 
JRL
since a state machine that isn't immutable outside of transitions isn't a state machine (that's basically part of the definition)
 
State machines limit the transitions that can exist.
 
JRL
@Crell yeah. i think better in state than stack, so i tend to favor OOP over FP.
 
Writing OOP is an FP-ish way, but not being religious about no-mutation, can get you a really far way. 85% purity is about as high as you can go before it really hurts.
Combine that with function (callable) composition rather than deep calls, which FP excels at, and you've got a really nice balance.
 
@Crell Excluding embedded DSL evaluators ;D
 
10:29 PM
DSLs are their own entire field. :-)
 
embedded DSLs
If you have a strictly typed embedded DSL, you can also limit allowed states. That's why they do "generalized algebraic data-types". But I never used it myself.
The limit is at compile-time, that is
 
JRL
i should say, a state machine isn't an OOP specific concept, it just fits with OOP features well. Using a DSL would still be using a state machine
just with more steps and overhead
 
Right. A state machine is a general concept. Objects/enums and methods are just a very convenient way to implement them.
@OlleHärstedt Well, I mean, once you have enums, you have state-limitation. That's kind of the whole point.
 
@Crell I was about to say. But then you have multiple enum variables, that might have some valid combinations, and some invalid.
 
At which point you should make a new enum.
 
10:38 PM
And you normally can't express those limitations in the type-system
New enum?
 
The classic example: Airlock, inner door and outer door, both can be open or closed.

This means... oh look, both open is a thing, well that's bad.

So you instead define its states as "OpenClosed", "ClosedOpen" ,"ClosedClosed". That's one enum/state machine.
 
To continue with the email example, you can have state for verified/unverified address, allowed subject, allowed body, etc
Hm
 
JRL
it was a user example, not an email one :)
 
Or if you also have to consider pressurized or not, you have states OpenClosedPressure, ClosedClosedPressure, ClosedClosedNoPressure, ClosedOpenNoPressure. Any other combination is "Dead", so you don't even define them.
 
@JRL Oh ok, sorry ^^
 
JRL
10:40 PM
we were just using that user's email to perform the verification since that's a common workflow in websites
no worries. FSM is so simple that it's hard to grasp, which i find is often the case in computation theory
 
Yeah, and the greek names no one can agree on doesn't help matters. :-)
 
JRL
any examples that might help understand are cluttered with extra stuff
 
@Crell So how do you break that down in enums?
 
JRL
automata you mean? :)
 
Or is that the enums?
 
10:41 PM
@OlleHärstedt Literally exactly that. All of those CamelCase words are an enum case.
 
So how do you ask the state if a door is open or not...?
I'm gonna be super-lazy and just ask the OCaml forum to write that out in GADT for me...
 
And then you define a transition from OpenClosedPressure to ClosedClosedPressure. But you don't define one from OpenClosedPressure to ClosedOpenNoPressure, because you don't want to go that way in one step. You must go through the others first to ensure the door closes, THEN you depressurize, THEN you open the outer door.
GADT?
 
GADT = generalized algebraic data-types
 
It's literally just an enum...? You know how to write those.
 
But still, how do you extract information from such an enum? I wanna know if the first door is open or closed. I have to match on all the enums then?
 
JRL
10:45 PM
why wouldn't you jsut define a method on the enum to query the state of each door?
 
Because then I have to change all such functions every time the enum changes.
So it's pretty fragile for extension
 
JRL
what? why?
 
Using crells example enum, I mean
 
enum Door
{
  case OpenClosedPressure;
  case ClosedClosedPressure;
  case ClosedClosedNoPressure;
  case ClosedOPenNoPressure.

  public function innerOpen(): bool
  {
    return $this == self::OpenClosedPressure;
  }

  public function outerOpen(): bool
  {
    // ...
  }
}
 
JRL
right, if your possible states change, then you have to update your state machine
 
10:46 PM
Ya
So, broken :D
 
JRL
i don't... see what the problem is with that
 
An enum or state machine is not open for extension. That is its entire purpose. To represent the complete universe of possible values and the rules for changing between them. If your problem space changes, you should be changing the enum and code around it.
 
Wait, lemme get the FP example
 
JRL
that's all code ever made
 
@Crell By "extension" I mean, changed by the developer, because the spec changed, and we now have another state to think about. :)
 
JRL
10:49 PM
how would you write code that is pre-emptively aware of a previously undefined state?
 
@OlleHärstedt I would expect the code to change then. Like, say, you add a "Disinfected" attribute (bathe the airlock with UV light or something). That's... a new system, so yes, you'll need to update the enums.
"Make invalid states unrepresentable."
With sealed classes you could go a step further and have a OpenClosedPressure class, with method closeInnerDoor(), which returns an instance of ClosedClosedPressure.
 
It's really late in EU, so I might not get/write the FP version of such state machine tonight... ^^
Sadly, all examples for GADT in existence are using abstract syntax-trees to represent invalid state. Like, adding two bools together, when you're only allowed to add two ints or floats.
https://dev.realworldocaml.org/gadts.html
But I think the solution would be a DSL of a kind, where the operation Depressurize won't compile in an invalid state (inner door open).
@Crell Also, this method won't scale well as the number of states increases. :)
@JRL The code would have to use a "whitelist" type of thing instead of blacklisting invalid states, I guess.
 
You don't need a DSL here. You could use one, I suppose, but it's really unnecessary here.
 
Compare with
type _ value =
| Int : int -> int value
| Bool : bool -> bool value

type _ expr =
| Value : 'a value -> 'a expr
| Eq : int expr * int expr -> bool expr
| Plus : int expr * int expr -> int expr
| If : bool expr * 'a expr * 'a expr -> 'a expr
"Plus" is only applicable on int expr
Trying to convert this to doors :D
Hm, but what I can see, the same limitation happens - have to change the entire machine for each new state
Have to sleep. To be continued. :D \o
 

« first day (4374 days earlier)      last day (564 days later) »