Not All Objects Serializable

The question I have

I was reading this article “PHP Serialization, Stack Traces, and Exceptions”[^1] and had these questions:

  • Why PDO isn’t serializable?
  • Why isn’t every type of object serializable?

The answer

After some search, I found some useful information as follows. check this for details

A PDO object contains state that cannot be represented in the serialization format. For example, the PDO object contains an open connection to a database server.

And I copied the answer from here:

Some objects encapsulate resources like file pointers or network sockets that can’t be deserialized to the state they were in when you serialized the object that contained them.

Example: you shouldn't deserialize an object that serves as an authenticated database connection, because to do so, you'd need the serialized form to contain a plaintext password. This would not be a good practice, because someone might get a hold of the saved serialized form. You also have no idea when you deserialize that the database server is still running, can be accessed, the authentication credentials still valid, etc.

Why the test written by the author can work

Test code is here:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class NotSerializable implements Serializable
{
  public function serialize()
  {
    throw new LogicException('You cannot serialize or unserialize NotSerializable instances');
  }

  public function unserialize($serialized)
  {
    throw new LogicException('You cannot serialize or unserialize NotSerializable instances');
  }
}

If you write a test code like this:

1
2
3
4
$a = new NotSerializable();

echo serialize($a);

It will throw the error:

1
2
3
4
5
6
7
PHP Fatal error:  Uncaught LogicException: You cannot serialize or unserialize NotSerializable instances in /home/jdoodle.php:7
Stack trace:
#0 [internal function]: NotSerializable->serialize()
#1 /home/jdoodle.php(23): serialize(Object(NotSerializable))
#2 {main}
  thrown in /home/jdoodle.php on line 7
Command exited with non-zero status 255

My understanding

Because the serialize() function should return string or null to represent the object’s state, but it throws an Exception which isn’t it’s state. Thus in this scenario the object of class NotSerializable is not serializable even though it implements the Serializable interface.

Reference

[^1] PHP Serialization, Stack Traces, and Exceptions

PHP Magic Method Invoke

What did I see?

I saw in a PHP class using this method __invoke

1
2
3
4
public function __invoke(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
{
    ...
}

Why using this method?

This magic method is called when user tries to invoke object as a function. Possible use cases may include some approaches like functional programming or some callbacks.

How do people use it?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Invokable
{
    /**
     * This method will be called if object will be executed like a function:
     *
     * $invokable();
     *
     * Args will be passed as in regular method call.
     */
    public function __invoke($arg, $arg, ...)
    {
        print_r(func_get_args());
    }
}

// Example:
$invokable = new Invokable();
$invokable([1, 2, 3]);

// optputs:
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

Render Code Block in Bootstrap 5

Issue found:

I found the post which has code block cannot render correctly in the small devices which width is less than 576px.

issue-rendering-my-code-block-in-bootstrap-5

Why?

I want to understand why this happend. So I just picked up a snippet which contains the code block and put into the bootstrap 5 example. Then the render result is like this:

render code block in bootstrap 5

My code block looks like this:

1
2
3
4
5
6
7
8
9
  <div class="highlight">
    <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
      <code class="language-bash" data-lang="bash">
        <span>export ANDROID_HOME</span><span style="color:#f92672">=</span><span>/Users/aron/Library/Android/sdk</span>
        export PATH<span style="color:#f92672">=</span>$ANDROID_HOME/platform-tools:$PATH
        export PATH<span style="color:#f92672">=</span>$ANDROID_HOME/tools:$PATH
    </code>
    </pre>
  </div>

This doesn’t have issue, So I guess my html dom may have issue, so I copied my html dom just one more level up

1
2
3
4
5
6
7
  <div class="d-grid">  
  <div class="highlight">
    <pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
    ...
    </pre>
  </div>
</div>

then the issue happens:

issue with code block

So I know the issue happen when I use class="d-grid", and checked again the class d-grid using this style:

1
2
3
.d-grid {
    display: grid !important;
}

and class pre using this style

1
2
3
4
5
6
7
pre {
    display: block;
    margin-top: 0;
    margin-bottom: 1rem;
    overflow: auto;
    font-size: .875em;
}

If I tick off class d-grid display style, it will render correctly.

How to solve it

remove the class d-grid

Phpstorm

phpstorm Mac Keymap

shortcut description
Ctrl Option O Optimize imports
Ctrl G Next selection
Ctrl Cmd G All selection
Cmd D Duplicate code
Option Cmd L Reformat code

How to stop indexing

System Settings -> Synchronize external changes when switching to the IDE window or opening an editor tab

Android app with react native

Issue I met

I want to follow this article to learn creating apps with react native. But I met several issues:

error Failed to launch emulator. Reason: No emulators found as an output of emulator -list-avds. warn Please launch an emulator manually or connect a device. Otherwise app may fail to launch. info Installing the app… Task :app:installDebug FAILED

Then I realized the another issue prompted: /bin/sh: adb: command not found

How to solve

After googling, I got answer:

1
2
3
export ANDROID_HOME=/Users/aaron/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
export PATH=$ANDROID_HOME/tools:$PATH

and I ran it again, it works. But I think you should also have created an AVD as well.