Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function-scoped static variable example to New in initializers docs #4431

Closed
wants to merge 1 commit into from

Conversation

imaarov
Copy link
Contributor

@imaarov imaarov commented Jan 31, 2025

I think demonstrating a static variable that holds an object in a function is essential to include in this document, for example :

<?php

class Foo {
	public string $name;
	public function __construct(string $name) {
		$this->name = $name;
	}
}

function testStatic(?string $entryName = NULL) {
    static $x = new Foo($entryName);
	echo $x->name, PHP_EOL;
    
}
testStatic('iman'); // output : iman
testStatic();       // output : iman (as the object type is static in function, it retains its state) 

@imaarov imaarov closed this Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant