DeveloperBreeze

PHP Class and Object Example

// 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();

Continue Reading

Discover more amazing content handpicked just for you

Tutorial
javascript

Understanding JavaScript Classes

class Animal {
  constructor(type, name) {
    this.type = type;
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

const dog = new Animal('Dog', 'Buddy');
dog.speak(); // Output: Buddy makes a noise.

Inheritance allows a class to extend another class, inheriting its properties and methods while adding new ones or overriding existing ones.

Sep 02, 2024
Read More
Code
javascript

JavaScript Class with Constructor and Method

No preview available for this content.

Jan 26, 2024
Read More
Code
python

Class with Method

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!