---
title: "Method Overriding"
description: "Method overriding example for area calculation in php"
url: "https://www.geekboots.com/php/method-overriding"
markdown_url: "https://www.geekboots.com/php/method-overriding/md"
category: "PHP Scripting"
author: "Geekboots"
published: "2016-06-06"
updated: "2026-11-15T16:53:06.614Z"
tags: ["php", "method overriding", "oop"]
---

# Method Overriding (PHP Scripting)

> Method overriding example for area calculation in php

## Source Code

### method-overriding.php

```php
<?php
	/* Parent class */
	class square{
		public $height = 0;
		function __construct($h) {
			$this->height = $h;
		}
		
		function area() {
			return ($this->height * $this->height);
		}
	}
	
	/* Child class */
	class rectangle extends square {
		public $width = 0;
		function __construct($h, $w) {
			$this->height = $h;
			$this->width = $w;
		}
		
		/* Declare method with same name in child class */
		function area() {
			return ($this->height * $this->width);
		}
	}
	
	$sq = new square(6);
	echo "Area of a Square: ".$sq->area();
	
	$rec = new rectangle(8,4);
	echo "<br />Area of a Rectangle: ".$rec->area();
?>



/* Output */
Area of a Square: 36
Area of a Rectangle: 32
```

## Related Code Examples

- [Fetch MySQL Data](https://www.geekboots.com/php/fetch-mysql-data) ([Markdown](https://www.geekboots.com/php/fetch-mysql-data/md))
- [Imagick](https://www.geekboots.com/php/imagick) ([Markdown](https://www.geekboots.com/php/imagick/md))
- [Read XML File](https://www.geekboots.com/php/read-xml-file) ([Markdown](https://www.geekboots.com/php/read-xml-file/md))
- [Create XML File](https://www.geekboots.com/php/create-xml-file) ([Markdown](https://www.geekboots.com/php/create-xml-file/md))
- [Read CSV file](https://www.geekboots.com/php/read-csv) ([Markdown](https://www.geekboots.com/php/read-csv/md))
- [Read File](https://www.geekboots.com/php/read-file) ([Markdown](https://www.geekboots.com/php/read-file/md))
- [Copy File](https://www.geekboots.com/php/copy-file) ([Markdown](https://www.geekboots.com/php/copy-file/md))
- [Create Delete File](https://www.geekboots.com/php/create-delete-file) ([Markdown](https://www.geekboots.com/php/create-delete-file/md))

---
Source: [Geekboots](https://www.geekboots.com/php/method-overriding)
