Imagine this scenario:
class Page {}
class Book {
private $pages = array();
public function __construct() {}
public function addPage($pagename) {
array_push($this->pages, new Page($pagename));
}
}
Is there anyway i can make sure only objects of my class Book can instantiate Page? Like, if the programmer tries something like:
$page = new Page('pagename');
the script throws an exception?
Thanks
It's a bit contrived, but you could use this:
abstract class BookPart
{
abstract protected function __construct();
}
class Page
extends BookPart
{
private $title;
// php allows you to override the signature of constructors
protected function __construct( $title )
{
$this->title = $title;
}
}
class Book
extends BookPart
{
private $pages = array();
// php also allows you to override the visibility of constructors
public function __construct()
{
}
public function addPage( $pagename )
{
array_push( $this->pages, new Page( $pagename ) );
}
}
$page = new Page( 'test will fail' ); // Will result in fatal error. Comment out to make scrip work
$book = new Book();
$book->addPage( 'test will work' ); // Will work.
var_dump( $book );
Well, I see your point, but with the tools provided by the language, this is not possible.
One thing you could do, is require a Book object when constructing a Page:
class Page {
public function __construct( Book $Book ) {}
}
class Book {
public function addPage() {
$this->pages[] = new Page( $this );
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With