# How to Customize the Names of Generated Sitemap Files in PHP‑Sitemap‑Generator

> Learn to customize generated sitemap file names in PHP-Sitemap-Generator by subclassing, editing source, or using reflection when the public API falls short.

- Repository: [Mehmet Selcuk Batal/php-sitemap-generator](https://github.com/msbatal/php-sitemap-generator)
- Tags: how-to-guide
- Published: 2026-03-07

---

**You cannot customize filenames through the public API, but you can override the private properties `$sitemapFile`, `$sitemapIndexFile`, and `$robotsFile` in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) by subclassing the class, editing the source directly, or using PHP Reflection.**

The PHP‑Sitemap‑Generator library by msbatal provides a straightforward way to create XML sitemaps, but it does not expose public setters for output filenames. If you need to customize the names of generated sitemap files beyond the defaults, you must work with the class's internal property structure as implemented in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php).

## Default File Naming Convention in SunSitemap

Inside [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php), three **private** properties control the output filenames:

- Line 25: `$sitemapFile = "sitemap.xml"`
- Line 31: `$sitemapIndexFile = "sitemap-index.xml"`
- Line 37: `$robotsFile = "robots.txt"`

These properties are referenced throughout the class (notably around lines 218‑236 and 295‑303) to determine the final file paths when `createSitemap()` and `updateRobots()` execute. Because the constructor only accepts parameters for **base URL**, **relative path**, **maximum URLs per file**, and **zip creation flag**, you cannot pass custom names during instantiation.

## Three Methods to Customize Sitemap File Names

### Subclass SunSitemap (Recommended)

The cleanest approach is extending `SunSitemap` and redeclaring the filename properties as `protected` with your custom values. PHP allows child classes to override parent property defaults, and since the parent methods access `$this->propertyName`, they automatically use your new values.

```php
<?php
require_once 'SunSitemap.php';

class CustomSitemap extends SunSitemap
{
    protected $sitemapFile = 'custom-sitemap.xml';
    protected $sitemapIndexFile = 'custom-index.xml';
    protected $robotsFile = 'custom-robots.txt';
}

$sitemap = new CustomSitemap('https://example.com', 'sitemaps/');
$sitemap->addUrl('home')
        ->addUrl('about')
        ->createSitemap()
        ->updateRobots();
?>

```

### Modify the Source Code Directly

For project‑specific forks where you maintain the codebase, edit the literal string values in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php):

```php
// Line 25
private $sitemapFile = "my-sitemap.xml";

// Line 31
private $sitemapIndexFile = "my-sitemap-index.xml";

// Line 37
private $robotsFile = "my-robots.txt";

```

This approach requires no subclassing, but you must track these changes when updating the library via Composer.

### Use PHP Reflection (Advanced)

When you cannot modify the class or create subclasses (e.g., when using a packaged phar archive), use `ReflectionClass` to modify the private properties at runtime:

```php
<?php
$sitemap = new SunSitemap('https://example.com', 'sitemaps/');

$ref = new ReflectionClass($sitemap);

$sitemapProp = $ref->getProperty('sitemapFile');
$sitemapProp->setAccessible(true);
$sitemapProp->setValue($sitemap, 'custom.xml');

$indexProp = $ref->getProperty('sitemapIndexFile');
$indexProp->setAccessible(true);
$indexProp->setValue($sitemap, 'custom-index.xml');

$robotsProp = $ref->getProperty('robotsFile');
$robotsProp->setAccessible(true);
$robotsProp->setValue($sitemap, 'custom-robots.txt');

$sitemap->addUrl('home')->createSitemap()->updateRobots();
?>

```

## Summary

- **No public API exists** in `SunSitemap` to set custom filenames during instantiation.
- **Subclassing** is the preferred method: override `$sitemapFile`, `$sitemapIndexFile`, and `$robotsFile` as protected properties in your child class.
- **Direct modification** of [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php) lines 25, 31, and 37 works for single‑project forks.
- **PHP Reflection** can modify private properties at runtime but adds complexity and maintenance overhead.

## Frequently Asked Questions

### Can I pass custom filenames to the SunSitemap constructor?

No. According to the source code in [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php), the constructor signature only accepts `$baseURL`, `$relativePath`, `$maxURLsPerFile`, and `$createZip`. There are no parameters for filenames, and the class lacks public setter methods for these properties.

### Will subclassing SunSitemap break future updates?

Subclassing is the safest approach for maintainability. When you override the filename properties in a child class, you isolate your changes from the vendor code. Updates to the parent `SunSitemap` class will not affect your custom filenames unless the property names themselves change.

### Why are the filename properties private instead of protected?

The properties are declared as `private` on lines 25, 31, and 37 of [`SunSitemap.php`](https://github.com/msbatal/php-sitemap-generator/blob/main/SunSitemap.php), which prevents direct access from child classes. However, PHP allows subclasses to redeclare properties with the same name but different visibility (protected/public), effectively overriding the defaults while maintaining encapsulation of the original values.

### Does changing the sitemap filename affect SEO?

Search engines detect sitemaps through the [`robots.txt`](https://github.com/msbatal/php-sitemap-generator/blob/main/robots.txt) file or direct submission in search consoles. As long as `updateRobots()` references the correct filename (which it will when you customize the properties), changing from [`sitemap.xml`](https://github.com/msbatal/php-sitemap-generator/blob/main/sitemap.xml) to a custom name does not negatively impact SEO, provided you maintain consistent internal linking.