Published on January 26, 2024By DeveloperBreeze

// Define a Person class
class Person {
    // Private property to store the person's name
    private $name;

    // Constructor to initialize the person's name
    public function __construct($name) {
        $this->name = $name;
    }

    // Method to greet the person
    public function greet() {
        return 'Hello, my name is ' . $this->name;
    }
}

// Create an instance of the Person class with the name 'Alice'
$person = new Person('Alice');

// Call the greet method and echo the result
echo $person->greet();

Comments

Please log in to leave a comment.

Continue Reading:

Class with Method

Published on January 26, 2024

python

JavaScript Class with Constructor and Method

Published on January 26, 2024

javascript

Understanding JavaScript Classes: A Tutorial

Published on September 02, 2024

javascript