Discussion:
(Need help) Beginner's question
Sachin Raut
2014-07-31 08:03:51 UTC
Permalink
Hi

I have a question in this following code.
<?php

class A

{

public $country;

function __construct($data=array())

{

if(!isset($data))

{

trigger_error("Couldn't construct address with a ".get_class());

}

}

}

$obj=new A();

?>

I am not sending any "array" as an argument while creating an object. And
hence "if statement" in constructor should have been executed, but it
displays blank page. My query is why it's not executing "if statement"?

Thanks
Sachin
Stuart Dallas
2014-07-31 08:07:02 UTC
Permalink
Post by Sachin Raut
Hi
I have a question in this following code.
<?php
class A
{
public $country;
function __construct($data=array())
{
if(!isset($data))
{
trigger_error("Couldn't construct address with a ".get_class());
}
}
}
$obj=new A();
?>
I am not sending any "array" as an argument while creating an object. And
hence "if statement" in constructor should have been executed, but it
displays blank page. My query is why it's not executing "if statement"?
You are giving it a default value of an empty array, therefore the variable
is set. Either change the default to null or change the !isset($data) to
simply !$data.
--
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Jigar Dhulla
2014-07-31 08:07:26 UTC
Permalink
Hi,

$data will always be set. Check *if(empty($data)) *instead.
Post by Sachin Raut
Hi
I have a question in this following code.
<?php
class A
{
public $country;
function __construct($data=array())
{
if(!isset($data))
{
trigger_error("Couldn't construct address with a ".get_class());
}
}
}
$obj=new A();
?>
I am not sending any "array" as an argument while creating an object. And
hence "if statement" in constructor should have been executed, but it
displays blank page. My query is why it's not executing "if statement"?
Thanks
Sachin
--
Regards,
Jigar Dhulla
Domain nikha.org
2014-07-31 09:27:03 UTC
Permalink
Post by Sachin Raut
Hi
I have a question in this following code.
<?php
class A
{
public $country;
function __construct($data=array())
{
if(!isset($data))
{
trigger_error("Couldn't construct address with a ".get_class());
}
}
}
$obj=new A();
?>
I am not sending any "array" as an argument while creating an object. And
hence "if statement" in constructor should have been executed, but it
displays blank page. My query is why it's not executing "if statement"?
Thanks
Sachin
Hi,
because $data is set! In your argument in the constructor: $data = array()!
try if(count($data) == 0)...
Niklaus
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Simon Schick
2014-07-31 09:42:55 UTC
Permalink
Hi, Sachin

If you want your code to fail, you could set the default to NULL and check
for NULL.


<?php

class A

{

public $country;

function __construct($data=NULL)

{

if($data === NULL)

{

trigger_error("Couldn't construct address with a ".get_class());

}

}

}

$obj=new A();

?>

Bye,
Simon
Post by Sachin Raut
Hi
I have a question in this following code.
<?php
class A
{
public $country;
function __construct($data=array())
{
if(!isset($data))
{
trigger_error("Couldn't construct address with a ".get_class());
}
}
}
$obj=new A();
?>
I am not sending any "array" as an argument while creating an object. And
hence "if statement" in constructor should have been executed, but it
displays blank page. My query is why it's not executing "if statement"?
Thanks
Sachin
Continue reading on narkive:
Loading...