Link to Prompt
What the Prompt does
- Writes PHP 8 object oriented code using best practices
- If you don’t ask GPT to do this it won’t use OOP and will give you outdated code
- Outputs comments and instructions in [TARGETLANGUAGE] but only outputs code in English
Example Prompt Output GPT3.5
Input:
create a simple hello world script that shows off PHP 8 advanced features can do
Output:
Sure, I can help you with that!
Here’s a simple “Hello World” script that utilizes some advanced features of PHP 8 while also following the best practices you listed:
First, let’s create a file called HelloWorld.php
with the following content:
<?php declare(strict_types=1);
namespace MyNamespace;
class HelloWorld
{
private string $message;
public function __construct(string $message)
{
$this->message = $message;
}
public function getMessage(): string
{
return $this->message;
}
public function setMessage(string $message): void
{
$this->message = $message;
}
public function printMessage(): void
{
echo $this->message . PHP_EOL;
}
}
Let’s go over what we did:
- We started the file with
declare(strict_types=1)
to enable strict typing. - We defined a namespace for our code. In this case, we used
MyNamespace
. - We created a class called
HelloWorld
, which contains a private property called$message
. - We defined a constructor method that takes a string argument and sets it as the value of
$message
. - We defined a getter and a setter method for
$message
. - We defined a method called
printMessage
, which simply echoes the value of$message
.
Now let’s create a file called index.php
and include our HelloWorld.php
file:
<?php declare(strict_types=1);
require_once 'HelloWorld.php';
use MyNamespace\HelloWorld;
$message = 'Hello, world!';
$helloWorld = new HelloWorld($message);
$helloWorld->printMessage();
$newMessage = 'Hello, PHP 8!';
$helloWorld->setMessage($newMessage);
$helloWorld->printMessage();
Let’s go over what we did:
- We started the file with
declare(strict_types=1)
to enable strict typing. - We included our
HelloWorld.php
file usingrequire_once
. - We used the
use
statement to import theHelloWorld
class from theMyNamespace
namespace. - We created a string variable called
$message
and set it to'Hello, world!'
. - We created a new instance of the
HelloWorld
class, passing$message
as an argument to the constructor. - We called the
printMessage
method on the$helloWorld
object, which should output'Hello, world!'
. - We created a new string variable called
$newMessage
and set it to'Hello, PHP 8!'
. - We called the
setMessage
method on the$helloWorld
object, passing$newMessage
as an argument. - We called the
printMessage
method again, which should output'Hello, PHP 8!'
.
That’s it! This script follows the best practices you listed and also demonstrates some of the advanced features of PHP 8, such as typed properties, constructor property promotion, and the nullsafe operator.
Example Prompt Output GPT4
Input:
create a simple hello world script that shows off PHP 8 advanced features can do
Output:
Here’s a simple example of a “Hello World” script that demonstrates some PHP 8 advanced features and follows the best practices you provided:
HelloWorld.php
:
<?php
declare(strict_types=1);
namespace HelloWorld;
use DateTime;
class HelloWorld
{
public function __construct(
private string $message = 'Hello, World!'
) {}
public function setMessage(string $message): void
{
$this->message = $message;
}
public function getMessage(): string
{
return $this->message;
}
public function displayMessage(): void
{
echo $this->message . PHP_EOL;
}
}
class Helper
{
public static function getCurrentUnixTimestamp(): int
{
return (new DateTime())->getTimestamp();
}
}
index.php
:
<?php
declare(strict_types=1);
require_once 'HelloWorld.php';
use HelloWorld\HelloWorld;
use HelloWorld\Helper;
$helloWorld = new HelloWorld();
$helloWorld->displayMessage();
// Optional: Set a new message
$helloWorld->setMessage('Hello, PHP 8! Timestamp: ' . Helper::getCurrentUnixTimestamp());
$helloWorld->displayMessage();
In this example, I have created a HelloWorld
class following the best practices you provided, such as using PHP 8
Screenshot is only useful if it is about Emoji, Images or Mermaid charts rendered by ChatGPT,
in all other cases the text is better. Why? Because it’s indexable.
How I came up with it
I was tired of GTP always giving me old outdated code so I searched the internet for best practices for PHP 8 and put together a set of rules that seemed to fit the best practices I read from several places. I asked GPT how recent its knowledge was of different things to make sure the prompt didn’t ask it to do something it could not. For example I told it to use PSR-12 because that was the most recent one that GPT was aware of and it told me that it was what it thought should be used. I have written quite a few scripts with this and they have all worked very well.
More insights
PHP 8 oop code generator writes you modern PHP code that allows you to create classes that you can use over and over again and it also gives great comments to help you understand what the program does. If you found that the prompt can benefit others, Upvote it, to reach more people.