What surprised the Java shop when writing PHP

I know only Java and JavaScript (a little), so I decided to touch PHP at work, and before that, I tried to make an oleore framework after studying. At that time eh! I will draw roughly what I was surprised at and what I got into.

As a reminder, in this article, I'm in the Java area, and I just write down what I thought when I touched PHP, and insisted that PHP was bad or Java was good. I have no intention of doing so.

Concatenation of character strings is . (dot)

In Java, access the members of an object with ., likeinstance.member (). On the other hand, . In PHP is an operator that concatenates strings. I just wrote ʻobjcet.doSomething ()` and often wondered why it didn't work ...

Cannot overload function

This isn't just PHP, but functions can't be overloaded. There are default arguments, so use them well.

Parent class constructor is not explicitly called

This may have been the most shocking. In the Java world, it is a child class that has a parent class, so if the parent class is not initialized correctly, the child class cannot exist. It was shocking that a child instance could exist without the parent being initialized.

Return value false indicating failure

In PHP, false is often used as the return value to indicate a processing failure. If you fail to return a character string if it ends normally, it will return false, which makes your heart flutter when you hear it like Java, but that's what it is.

Array passed by value

In Java, if you pass an array as an argument, it will be passed by reference. The destination will affect the calling array. However, since it is passed by value by PHP standard, it does not affect the value of the caller. If you add & before the argument when defining a function like function callFunc (& $ array), it will be passed by reference. I feel that passing by price may be a bottleneck in performance, but I wonder if I don't have to worry too much → It seems that I don't have to worry too much (see comments).

There is no byte type

When doing byte processing in php, use a string type variable. It seems to swim in the binary sea by making full use of functions such as pack and unpack. It's quite difficult if you can't get used to it.

Internal encoding of strings

Since it is around the character string, next is here. The Java string is simple because it has UTF-16 content, but PHP's internal encoding seems to differ depending on the execution environment. If you don't write the code with that in mind, you'll soon get garbled hell.

There is no equivalent to private static final

There are several ways to represent constants in PHP. define and const. But this will be in public scope anyway. You can't make a closed constant only in the class. → It seems that you can make it with new PHP (see comments)

notice cannot be caught

I often see Notice hogehoge. If you think that an exception is thrown behind the scenes and you can transfer the processing to an appropriate handler if you catch it, you can't catch it. Of course there seems to be an alternative, but at first I was surprised.

Missing getSession (false)

From here, the story of JavaEE Session. There is no process equivalent to getSession (false) in PHP. It took some ingenuity to write the process "if the session hadn't started". ..

Objects cannot be stored in the session

Sessions can only carry strings or numbers. </ s> → Correctly, "cannot store reference type", so "only strings or numbers can be used" was not accurate. </ small>

If you want to put an object or array in a session, you need to serialize it before storing it. Unserialize to retrieve.

Session cookie expiration does not automatically extend </ s>

PHP can also set a session expiration date. However, the expiration date decided here is the time after the session starts. Suppose the session expires in 30 minutes. You created a session at time 0. When you access the screen again at 10 minutes, Java will extend the session expiration by 30 minutes at this point. In other words, the timeout time is 40 minutes, but the PHP cookie does not grow. It just times out at the first set time. You have to do a bit of work to get a Java-like session strategy. </ S> Here, there is a possibility that the understanding is a little fundamentally wrong, so investigate it before editing

Cookies do not disappear even if you delete the session

In php you can use the session_destroy function to destroy a session. However, this only erases the session data on the server side, and the session cookie on the browser side remains. To delete a session, you have to follow the procedure of discarding session data on the server side → deleting cookies in the browser.

User Contributed Notes The last part that went to the end. The PHP reference is called User Contributed Notes, and you can read tips written by actual users. I was envious of this culture.

Recommended Posts