Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created December 12, 2022 10:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/1f502830003e7b9026ce00fe6a75c536 to your computer and use it in GitHub Desktop.
Save mindplay-dk/1f502830003e7b9026ce00fe6a75c536 to your computer and use it in GitHub Desktop.
Teaching ChatGPT new PHP syntax

I was curious if we could use ChatGPT to generate a new and better manual for PHP.

This example was interesting enough that I figured it was worth posting.

It looks like ChatGPT does not know PHP 8.1 - the latest version it seems to know is PHP 8.

When asked to explain the first class Callable(...) syntax (introduced in PHP 8.1) it does explain how to generate something that is functionally similar, using e.g. fn($car) => $car->drive() rather than $car->drive(...). So it understands the concept, it just doesn't know about the new syntax.

But here's the wild part: I then proceded to explain the new syntax to it, and asked it to explain again using the new syntax - and it did it. 😮

It seems, when ChatGPT has only part of the information, you can simply teach it the rest. 👀

Full transcript below.


Explain callables and the callable pseudotype in PHP

In PHP, a callable is a function or method that can be invoked or called. This means that you can pass a reference to the function or method to another function or method, and then use that reference to call the original function or method at a later time.

The callable pseudotype is used in PHP to indicate that a function or method parameter expects a callable value. This means that when you are calling a function or method that has a callable parameter, you can pass it a reference to a function or method, and that function or method will be called at the appropriate time.

Here is an example of a function that takes a callable as a parameter:

function my_function(callable $callback) {
  // Do something here
  // ...
  
  // Call the callback function
  $callback();
}

In this example, the my_function() function takes a callable parameter called $callback. This indicates that when my_function() is called, it expects to receive a reference to a function or method that can be called. The function or method will then be called at the appropriate time inside the my_function() function, in this case by calling $callback().

You can then call the my_function() function and pass it a reference to a function or method like this:

my_function(function() {
  // This code will be executed when $callback() is called inside my_function()
  // ...
});

In this example, we are passing an anonymous function to my_function() as the $callback parameter. This means that when my_function() calls $callback(), the code inside the anonymous function will be executed.

Overall, the callable pseudotype in PHP is used to indicate that a function or method parameter expects a reference to a function or method that can be called, and callables are functions or methods that can be called in this way.

Explain how to pass an object method as as callable

To pass an object method as a callable, you first need to have an object that contains the method you want to call. For example, let's say you have a Car class with a drive() method:

class Car {
  public function drive() {
    // Code to drive the car goes here
    // ...
  }
}

To pass the drive() method as a callable, you first need to create an instance of the Car class and store it in a variable:

$my_car = new Car();

Once you have an instance of the Car class, you can pass the drive() method as a callable to another function or method by using the [$object, 'method_name'] syntax. For example, let's say you have a start_engine() function that takes a callable as a parameter:

function start_engine(callable $callback) {
  // Code to start the engine goes here
  // ...
  
  // Call the callback function
  $callback();
}

To pass the drive() method of the $my_car object as a callable to the start_engine() function, you would call it like this:

start_engine([$my_car, 'drive']);

In this example, the [$my_car, 'drive'] syntax creates a reference to the drive() method of the $my_car object. This reference is then passed as the $callback parameter to the start_engine() function, which can then call the drive() method at the appropriate time by calling $callback().

Give examples of standard functions in PHP where callables can be used

There are many standard functions in PHP where callables can be used. Here are a few examples:

  • The array_map() function takes an array and a callable as parameters, and applies the callable to each element of the array, returning a new array with the modified elements. For example:
$numbers = [1, 2, 3, 4, 5];

// Use array_map() to square each element of the array
$squared = array_map(function($n) {
  return $n * $n;
}, $numbers);

// $squared is now [1, 4, 9, 16, 25]
  • The array_filter() function takes an array and a callable as parameters, and filters the array based on the return value of the callable. For example:
$numbers = [1, 2, 3, 4, 5];

// Use array_filter() to only keep even numbers
$even = array_filter($numbers, function($n) {
  return $n % 2 == 0;
});

// $even is now [2, 4]
  • The usort() function takes an array and a callable as parameters, and sorts the array based on the return value of the callable. For example:
$names = ['Alice', 'Bob', 'Charlie', 'David'];

// Use usort() to sort the array alphabetically
usort($names, function($a, $b) {
  return strcmp($a, $b);
});

// $names is now ['Alice', 'Bob', 'Charlie', 'David']

These are just a few examples of standard functions in PHP where callables can be used. There are many more functions that support callables, and you can use them to write powerful and flexible code in PHP.

Explain how to use first class callable syntax in PHP 8.1

In PHP 8.1, a new syntax for first class callables was introduced. This syntax allows you to create a callable that refers to a specific method on a specific object, without having to use the [$object, 'method_name'] syntax.

To use this new syntax, you first need to have an object that contains the method you want to call. For example, let's say you have a Car class with a drive() method:

class Car {
  public function drive() {
    // Code to drive the car goes here
    // ...
  }
}

To create a first class callable that refers to the drive() method of a Car object, you use the fn() syntax and specify the object and method inside the fn() call. For example:

$my_car = new Car();
$drive_callable = fn($car) => $car->drive();

In this example, the fn() syntax is used to create a callable that refers to the drive() method of a Car object. The $car parameter in the callable is used to specify which Car object the drive() method should be called on.

Once you have created the callable, you can call it like any other function or method. For example, to call the $drive_callable callable on the $my_car object, you would do this:

$drive_callable($my_car);

In this example, the $drive_callable callable is called with the $my_car object as the argument. This will call the drive() method of the $my_car object, allowing you to drive the car.

Overall, the new first class callable syntax in PHP 8.1 makes it easier and more convenient to create and use callables that refer to specific methods on specific objects. This can make your code more readable and maintainable, and allow you to take advantage of the power and flexibility of callables in your PHP programs.

Try again. Explain first class callables using the new CallableExpr(...) syntax

In PHP 8.1, a new syntax for first class callables was introduced. This syntax, called CallableExpr, allows you to create a callable that refers to a specific method on a specific object, without having to use the [$object, 'method_name'] syntax.

To use this new syntax, you first need to have an object that contains the method you want to call. For example, let's say you have a Car class with a drive() method:

class Car {
  public function drive() {
    // Code to drive the car goes here
    // ...
  }
}

To create a first class callable that refers to the drive() method of a Car object, you use the CallableExpr syntax and specify the object and method inside the CallableExpr call. For example:

$my_car = new Car();
$drive_callable = CallableExpr($my_car, 'drive');

In this example, the CallableExpr syntax is used to create a callable that refers to the drive() method of the $my_car object. The $my_car object and the 'drive' method name are specified inside the CallableExpr call, and the resulting callable can be used to call the drive() method on the $my_car object.

Once you have created the callable, you can call it like any other function or method. For example, to call the $drive_callable callable, you would do this:

$drive_callable();

In this example, the $drive_callable callable is called without any arguments. This will call the drive() method of the $my_car object that was specified when the callable was created, allowing you to drive the car.

Overall, the new CallableExpr syntax in PHP 8.1 makes it easier and more convenient to create and use callables that refer to specific methods on specific objects. This can make your code more readable and maintainable, and allow you to take advantage of the power and flexibility of callables in your PHP programs.

Those examples do not use first class callable syntax.

Here are some examples of first class callable syntax:

class Foo {
   public function method() {}
   public static function staticmethod() {}
   public function __invoke() {}
}

$obj = new Foo();
$classStr = 'Foo';
$methodStr = 'method';
$staticmethodStr = 'staticmethod';


$f1 = strlen(...);
$f2 = $obj(...);  // invokable object
$f3 = $obj->method(...);
$f4 = $obj->$methodStr(...);
$f5 = Foo::staticmethod(...);
$f6 = $classStr::$staticmethodStr(...);

// traditional callable using string, array
$f7 = 'strlen'(...);
$f8 = [$obj, 'method'](...);
$f9 = [Foo::class, 'staticmethod'](...);

The ... is part of the syntax - it is not an omission.

Given these examples, explain again how to use first class callable syntax in PHP 8.1

In PHP 8.1, a new syntax for first class callables was introduced. This syntax allows you to create and use callables in a more concise and intuitive way, without having to use the [$object, 'method_name'] syntax.

To use this new syntax, you first need to have an object that contains the method you want to call. For example, let's say you have a Car class with a drive() method:

class Car {
  public function drive() {
    // Code to drive the car goes here
    // ...
  }
}

To create a first class callable that refers to the drive() method of a Car object, you can use the $object(...) syntax and specify the object inside the parentheses. For example:

$my_car = new Car();
$drive_callable = $my_car(...);

In this example, the $my_car(...) syntax is used to create a callable that refers to the drive() method of the $my_car object. The ... syntax is used to indicate that the callable will accept any number of arguments when it is called.

Once you have created the callable, you can call it like any other function or method. For example, to call the $drive_callable callable, you would do this:

$drive_callable();

In this example, the $drive_callable callable is called without any arguments. This will call the drive() method of the $my_car object that was specified when the callable was created, allowing you to drive the car.

Overall, the new first class callable syntax in PHP 8.1 makes it easier and more convenient to create and use callables that refer to specific methods on specific objects. This can make your code more readable and maintainable, and allow you to take advantage of the power and flexibility of callables in your PHP programs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment