Discussion:
invoking unrelated constructor's
Sachin Raut
2013-11-22 13:33:52 UTC
Permalink
Query relate to constructors


Dear Friends,

I am reading PHP book & it says that unrelated constructors can be invoked
as follows.


<?php

class A

{

function __construct()

{

echo "This is A's constructor";

}

}

class B

{

function __construct()

{

A::__construct();

echo "This is B's constructor";

}

}

$obj=new B();

?>


But i am getting an error "Non-static method A::__construct() cannot be
called statically".


Can someone please throw some light if there is anything i am missing in
above code.


Thanks

Sachin Raut.
Stuart Dallas
2013-11-22 13:36:36 UTC
Permalink
Post by Sachin Raut
Query relate to constructors
Dear Friends,
I am reading PHP book & it says that unrelated constructors can be invoked
as follows.
<?php
class A
{
function __construct()
{
echo "This is A's constructor";
}
}
class B
{
function __construct()
{
A::__construct();
echo "This is B's constructor";
}
}
$obj=new B();
?>
But i am getting an error "Non-static method A::__construct() cannot be
called statically".
Can someone please throw some light if there is anything i am missing in
above code.
Your PHP book is out of date. Methods must now be declared as static if they are to be called statically, otherwise you cannot call them statically.

The more important question is why the heck you would want to do what you’re trying to do? Calling another class’s constructor seems like a really really really bad idea, not to mention completely daft.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sachin Raut
2013-11-22 14:03:03 UTC
Permalink
Thanks Stuart. Will remember your advise.

Sachin Raut
Post by Sachin Raut
Post by Sachin Raut
Query relate to constructors
Dear Friends,
I am reading PHP book & it says that unrelated constructors can be
invoked
Post by Sachin Raut
as follows.
<?php
class A
{
function __construct()
{
echo "This is A's constructor";
}
}
class B
{
function __construct()
{
A::__construct();
echo "This is B's constructor";
}
}
$obj=new B();
?>
But i am getting an error "Non-static method A::__construct() cannot be
called statically".
Can someone please throw some light if there is anything i am missing in
above code.
Your PHP book is out of date. Methods must now be declared as static if
they are to be called statically, otherwise you cannot call them statically.
The more important question is why the heck you would want to do what
you’re trying to do? Calling another class’s constructor seems like a
really really really bad idea, not to mention completely daft.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Stuart Dallas
2013-11-22 14:15:12 UTC
Permalink
Post by Sachin Raut
Thanks Stuart. Will remember your advise.
Great, but you didn’t answer the question. What you’re looking to do here is really bad design, and there’s definitely a better way of doing it.

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Post by Sachin Raut
Post by Sachin Raut
Query relate to constructors
Dear Friends,
I am reading PHP book & it says that unrelated constructors can be invoked
as follows.
<?php
class A
{
function __construct()
{
echo "This is A's constructor";
}
}
class B
{
function __construct()
{
A::__construct();
echo "This is B's constructor";
}
}
$obj=new B();
?>
But i am getting an error "Non-static method A::__construct() cannot be
called statically".
Can someone please throw some light if there is anything i am missing in
above code.
Your PHP book is out of date. Methods must now be declared as static if they are to be called statically, otherwise you cannot call them statically.
The more important question is why the heck you would want to do what you’re trying to do? Calling another class’s constructor seems like a really really really bad idea, not to mention completely daft.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sachin Raut
2013-11-22 15:29:17 UTC
Permalink
I am learning PHP & came across that code in the book, hence thought to
clear the doubt. Inheritance would have solved that issue.

Could you please tell me which "design patterns" are recommended.

Thanks
Sachin Raut
Post by Sachin Raut
Thanks Stuart. Will remember your advise.
Great, but you didn’t answer the question. What you’re looking to do here
is really bad design, and there’s definitely a better way of doing it.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Post by Sachin Raut
Post by Sachin Raut
Query relate to constructors
Dear Friends,
I am reading PHP book & it says that unrelated constructors can be
invoked
Post by Sachin Raut
Post by Sachin Raut
as follows.
<?php
class A
{
function __construct()
{
echo "This is A's constructor";
}
}
class B
{
function __construct()
{
A::__construct();
echo "This is B's constructor";
}
}
$obj=new B();
?>
But i am getting an error "Non-static method A::__construct() cannot be
called statically".
Can someone please throw some light if there is anything i am missing
in
Post by Sachin Raut
Post by Sachin Raut
above code.
Your PHP book is out of date. Methods must now be declared as static if
they are to be called statically, otherwise you cannot call them statically.
Post by Sachin Raut
The more important question is why the heck you would want to do what
you’re trying to do? Calling another class’s constructor seems like a
really really really bad idea, not to mention completely daft.
Post by Sachin Raut
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Sebastian Krebs
2013-11-22 15:46:23 UTC
Permalink
Post by Sachin Raut
I am learning PHP & came across that code in the book, hence thought to
clear the doubt. Inheritance would have solved that issue.
Could you please tell me which "design patterns" are recommended.
That depends on which problem you actually want to solve.
Post by Sachin Raut
Thanks
Sachin Raut
Post by Sachin Raut
Thanks Stuart. Will remember your advise.
Great, but you didn’t answer the question. What you’re looking to do here
is really bad design, and there’s definitely a better way of doing it.
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
Post by Sachin Raut
Post by Sachin Raut
Query relate to constructors
Dear Friends,
I am reading PHP book & it says that unrelated constructors can be
invoked
Post by Sachin Raut
Post by Sachin Raut
as follows.
<?php
class A
{
function __construct()
{
echo "This is A's constructor";
}
}
class B
{
function __construct()
{
A::__construct();
echo "This is B's constructor";
}
}
$obj=new B();
?>
But i am getting an error "Non-static method A::__construct() cannot
be
Post by Sachin Raut
Post by Sachin Raut
called statically".
Can someone please throw some light if there is anything i am missing
in
Post by Sachin Raut
Post by Sachin Raut
above code.
Your PHP book is out of date. Methods must now be declared as static if
they are to be called statically, otherwise you cannot call them
statically.
Post by Sachin Raut
The more important question is why the heck you would want to do what
you’re trying to do? Calling another class’s constructor seems like a
really really really bad idea, not to mention completely daft.
Post by Sachin Raut
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
github.com/KingCrunch
Mattias Thorslund
2013-11-22 18:33:12 UTC
Permalink
Post by Sebastian Krebs
Post by Sachin Raut
I am learning PHP & came across that code in the book, hence thought to
clear the doubt. Inheritance would have solved that issue.
Could you please tell me which "design patterns" are recommended.
That depends on which problem you actually want to solve.
All of them!! Who wants problems?

Happy Friday,

Mattias
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sachin Raut
2013-11-22 18:47:46 UTC
Permalink
Hi Dai Wang,

You are invoking "related" constructor (you are calling parent's
constructor) whereas i was asking about invoking "unrelated" constructor.
In my code 2 classes are not related to each other.

I received good knowledge from my fellow members.

Thanks
Sachin Raut
Post by Mattias Thorslund
Post by Sachin Raut
I am learning PHP & came across that code in the book, hence thought to
Post by Sachin Raut
clear the doubt. Inheritance would have solved that issue.
Could you please tell me which "design patterns" are recommended.
That depends on which problem you actually want to solve.
All of them!! Who wants problems?
Happy Friday,
Mattias
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...