<?php 
class X { 
  function X(){ 
    if($_POST['break']=='yes'){return;} 
    } 
  } 

class Y extends X{ 
  function Y(){ 
    parent::__construct(); 
    print("Y_constructor_finished"); 
    } 
  } 

$_POST['break']=='yes'; 
$new_Y=new Y();  // will print "Y_constructor_finished"

i does't work with return
it still print Y_constructor_finished

i does't work with return
it still print Y_constructor_finished

if you want to stop the execution at return statement,
place break statement there..

<?php 
class X { 
  function X(){ 
    if($_POST['break']=='yes')
    {
      //return;
      break;
    } 
    } 
  } 

class Y extends X{ 
  function Y(){ 
    parent::__construct(); 
    print("Y_constructor_finished"); 
    } 
  } 

$_POST['break']=='yes'; 
$new_Y=new Y();  // will print "Y_constructor_finished"

Naming your constructor method the same as the class, although not technically incorrect, should be avoided.

With PHP 5 you have the __construct() method for this particular purpose. When a class can not find a defined __construct() method it will, for backwards compatibility reasons, look for a function named identical to the class to use as a constructor.

**As of PHP 5.3.3 the backwards compatibility of naming a constructor the same as the class has been removed.

That aside, if you're going to do this properly, you should be returning a value from the constructor. The catch here is that this return value is ONLY available when the constructor is called directly. e.g. parent::__construct() or $obj->__construct()

In the case of initializing an object you will always get the instance of the object.

<?php 
class X 
{ 
	function __construct()
	{ 
		if( $_POST['break'] == 'yes' ){
			return false;
		} 
		return true;
	} 
} 
 
class Y extends X
{ 
	function __construct()
	{ 
		if( true === parent::__construct() ){
			print("Y_constructor_finished"); 
		}
	} 
} 
 
$_POST['break'] = 'no'; 
$new_Y=new Y();  // will print "Y_constructor_finished";

http://www.php.net/manual/en/language.oop5.decon.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.