<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Neloy Ahmed</title>
    <description>The latest articles on Forem by Neloy Ahmed (@neloyahmed).</description>
    <link>https://forem.com/neloyahmed</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F726494%2F4202867c-212d-41f6-92aa-7a7c4d84992b.jpg</url>
      <title>Forem: Neloy Ahmed</title>
      <link>https://forem.com/neloyahmed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/neloyahmed"/>
    <language>en</language>
    <item>
      <title>The story of readonly classes and properties in php</title>
      <dc:creator>Neloy Ahmed</dc:creator>
      <pubDate>Tue, 20 Dec 2022 04:14:08 +0000</pubDate>
      <link>https://forem.com/neloyahmed/the-story-of-readonly-classes-and-properties-in-php-1l02</link>
      <guid>https://forem.com/neloyahmed/the-story-of-readonly-classes-and-properties-in-php-1l02</guid>
      <description>&lt;p&gt;PHP 8.2 introduced the new feature of &lt;code&gt;readonly&lt;/code&gt; classes. Before learning about readonly classes let's have a flashback and discuss readonly properties so that we can grasp the idea properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Readonly properties:
&lt;/h3&gt;

&lt;p&gt;PHP 8.1 introduced the feature of readonly property. The concept of &lt;code&gt;readonly&lt;/code&gt; property in PHP is as simple as when a class property is declared using the &lt;code&gt;readonly&lt;/code&gt; modifier you will not be able to modify the property after initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

class User 
{
   public readonly string $name;

   public function __construct(string $name) {
       // Legal initialization.
       $this-&amp;gt;name = $name;
   }
}

$john = new User("John");
// Legal read.
var_dump($john-&amp;gt;name); // string(4) "John"

// Illegal reassignment. It does not matter that the assigned value is the same.
$john-&amp;gt;name = "John";
// Error: Cannot modify readonly property User::$name
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are some important notes to remember about readonly properties in PHP.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can only apply &lt;code&gt;readonly&lt;/code&gt; modifier on &lt;a href="https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.typed-properties"&gt;typed properties&lt;/a&gt; . A readonly property without type constraints can be created using the &lt;a href="https://www.php.net/manual/en/language.types.mixed.php"&gt;Mixed&lt;/a&gt; type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can not assign &lt;code&gt;readonly&lt;/code&gt; modifier on static properties.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User 
{
 static readonly string $name;
// Fatal error: Static property User::$name cannot be readonly
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can initialize the &lt;code&gt;readonly&lt;/code&gt; property once, and only from the scope where it has been declared. Any other assignment or modification of the property will result in an Error exception.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
class User 
{
  public readonly string $name;
}

$john = new User;
// Illegal initialization outside of private scope.
$john-&amp;gt;name = "John";
// Error: Cannot initialize readonly property User::$name from global scope
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can not assign default value on &lt;code&gt;readonly&lt;/code&gt; properties, because a &lt;code&gt;readonly&lt;/code&gt; property with a default value is essentially the same as a constant, and thus not particularly useful.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

class User {
    // Fatal error: Readonly property User::$age cannot have default value
    public readonly int $age = 22;
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can not unset() &lt;code&gt;readonly&lt;/code&gt; properties once they are initialized. However, it is possible to unset a &lt;code&gt;readonly&lt;/code&gt; property prior to initialization, from the scope where the property has been declared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not only plain assignments, any type of  modification of &lt;code&gt;readonly&lt;/code&gt; property will also result in an Error exception:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

class User {
  public function __construct(
  public readonly int $age = 22,
  public readonly array $ary = [],
  ) {}
}

$john = new User;

// Cannot modify readonly property User::$age
$john-&amp;gt;age += 1;

// Cannot modify readonly property User::$age
$john-&amp;gt;age++;

// Cannot modify readonly property User::$age
++$john-&amp;gt;age;

// Cannot modify readonly property User::$ary
$john-&amp;gt;ary[] = 1;

// Cannot modify readonly property User::$ary
$john-&amp;gt;ary[0][] = 1;

// Cannot modify readonly property User::$age
$ref =&amp;amp; $john-&amp;gt;age;

// Cannot modify readonly property User::$age
$john-&amp;gt;age =&amp;amp; $ref;

// Cannot acquire reference to readonly property User::$age
foreach ($john as &amp;amp;$prop);
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;However, objects (or resources) stored in &lt;code&gt;readonly&lt;/code&gt; properties may still be modified internally:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

class User {
  public function __construct(public readonly object $userProfile) {}
}

$john = new User(new stdClass);

// Legal interior mutation.
$john-&amp;gt;userProfile-&amp;gt;gender = "Male";

// Illegal reassignment.
$john-&amp;gt;userProfile = new stdClass;

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Readonly classes:
&lt;/h3&gt;

&lt;p&gt;Now as we got the concept of &lt;code&gt;readonly property&lt;/code&gt; it will be very easier for us to understand &lt;code&gt;readonly classes&lt;/code&gt; in PHP.&lt;/p&gt;

&lt;p&gt;If we define a class as &lt;code&gt;readonly&lt;/code&gt; all the declared property of that class will automatically become &lt;code&gt;readonly&lt;/code&gt; property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;readonly class User
{
    public string $name;
    public string $gender;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, &lt;code&gt;User::$name&lt;/code&gt; and &lt;code&gt;User::$gender&lt;/code&gt; are now &lt;code&gt;readonly&lt;/code&gt; properties. To achieve this previously in PHP 8.1 we had to do something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User
{
    public readonly string $name;
    public readonly string $gender;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this observation, we can say that readonly classes are syntactic sugar that will make all the declared properties of that class &lt;code&gt;readonly&lt;/code&gt; properties.&lt;/p&gt;

&lt;p&gt;So, we have to remember that, all those important notes that we described about &lt;code&gt;readonly&lt;/code&gt; properties are still applicable here. Such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A &lt;code&gt;readonly&lt;/code&gt; class can not contain untyped or static properties.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;readonly&lt;/code&gt; class's property cannot have a default value. etc.&lt;/li&gt;
&lt;li&gt;Another important note to remember about readonly classes is that A readonly class can only be extended if, the child class is also a readonly class. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;That's all for today, thank you for reading.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/a-k-m-ashraful-haque-bhuiyan/"&gt;Find me on LinkedIn&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/neloy372"&gt;Find me on Twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>php</category>
      <category>newfeature</category>
    </item>
    <item>
      <title>How to run USSD code in Linux</title>
      <dc:creator>Neloy Ahmed</dc:creator>
      <pubDate>Fri, 15 Oct 2021 16:51:02 +0000</pubDate>
      <link>https://forem.com/neloyahmed/how-to-run-ussd-code-in-linux-50k1</link>
      <guid>https://forem.com/neloyahmed/how-to-run-ussd-code-in-linux-50k1</guid>
      <description>&lt;p&gt;Recently in one of my home PC I removed windows and installed Ubuntu 20.04.3 LTS. &lt;/p&gt;

&lt;p&gt;One of the very first challenges I faced was making sure I can connect to the internet using my D-Link DWM-157 modem.&lt;br&gt;
I was able to connect to the internet using my modem pretty easily. Now it's time to face the next one - I was not able to find a way to check my SIM balance or internet balance by running USSD code (e.g. *123# etc).&lt;/p&gt;

&lt;p&gt;After doing some google search I came to know about ‘Modem Manager GUI’ and I was so happy. Opened my terminal and simply typed:&lt;br&gt;
&lt;code&gt;sudo apt-get update&lt;br&gt;
sudo apt-get install modem-manager-gui&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Someone was smiling behind the scene and was whispering not so easy boy! Prepare for the next hurdle.&lt;/p&gt;

&lt;p&gt;I was so happy it is a very nice GUI, everything is here for me i just a single click away to check anything. Everything was working perfectly in the GUI except the USSD code !&lt;br&gt;
When i submit USSD code it refuses me with following error:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Modem Manager &amp;gt;= 0.7.0: GDBus.Error:org.freedesktop.ModemManager1.Error.Core.Aborted: USSD terminated by network&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I did not find any work around to solve this issue. &lt;/p&gt;

&lt;p&gt;Then I came to know about a command line application called "gammu".&lt;br&gt;
&lt;code&gt;sudo apt-get install gammu&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After installing gammu created the configuration file for gammu using the command: &lt;code&gt;gammu-detect &amp;gt; .gammurc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command created a hidden configuration file in the home folder. &lt;code&gt;ctrl+h&lt;/code&gt; to view hidden files.&lt;/p&gt;

&lt;p&gt;Opened the configuration file created and checked for the devices detected by gammu. There were devices listed as ttyUSB0, ttyUSB1, ttyUSB2 etc. ttyUSB0 to ttyUSB3 belongs to the D-Link modem.&lt;/p&gt;

&lt;p&gt;Disconnected from the internet to send ussd code (because it was required).&lt;/p&gt;

&lt;p&gt;Now to send a ussd, type the following command in the terminal: &lt;code&gt;gammu getussd "USSD_CODE"&lt;/code&gt;. For example &lt;code&gt;gammu getussd *125#&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But each time I ran that command after waiting for some time I was refused by this error message: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;No response in specified timeout. Probably the phone is not connected. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Although my modem was connected properly.&lt;/p&gt;

&lt;p&gt;I also tried with adding the device number along with the above command ie, &lt;code&gt;gammu 1 getussd *125#&lt;/code&gt; or &lt;code&gt;gammu 2 getussd *125#&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;But i was continuously getting follow messages:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Warning: No configuration file found!&lt;br&gt;
Error: Failed to read [gammu1] section from configuration file (gammurc)!&lt;br&gt;
Warning: No configuration read, using builtin defaults!&lt;br&gt;
No response in specified timeout. Probably the phone is not connected.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;End of another frustrating tryout!&lt;/p&gt;

&lt;p&gt;Then i came to know that i can send ussd code using picocom by giving AT commands. To install picocom: &lt;code&gt;sudo apt install picocom&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now again to issue the AT commands i have to disable internet connection to free the modem.&lt;/p&gt;

&lt;p&gt;Next i need to find out the various ports of the modem using: &lt;code&gt;ls /dev/ttyU*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Usually 3 ports will be returned :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;dialogue port (i.e. 1st port) is /dev/ttyUSB0&lt;br&gt;
audio  port (i.e. 2nd port) is /dev/ttyUSB1   (audio and microphone if supported)&lt;br&gt;
sms &amp;amp; internet aka data  port (i.e. 3rd port) is /dev/ttyUSB3&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I started picocom on the dialogue port:&lt;br&gt;
&lt;code&gt;sudo picocom -c /dev/ttyUSB0&lt;/code&gt;&lt;br&gt;
the -c means local echo on, making AT commands visible as i type.&lt;/p&gt;

&lt;p&gt;Now check USSD:&lt;br&gt;
&lt;code&gt;AT+CUSD=1,"*333#"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But success is yet one step away!!! I was not able to type properly in the terminal. So i changed port and and started picocom on other ports available from : &lt;code&gt;ls /dev/ttyU*&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo picocom -c /dev/ttyUSB1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And finally the success! Yesssssssss…&lt;br&gt;
Now i am able to run any ussd code using following format : &lt;br&gt;
&lt;code&gt;AT+CUSD=1,"*333#"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To quit picocom, first press &lt;code&gt;CTRL+a&lt;/code&gt; followed by a regular &lt;code&gt;x&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Thank you.&lt;/p&gt;

</description>
      <category>linux</category>
    </item>
  </channel>
</rss>
