Page 1 of 1
OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 5:04 am
by aaaaaaaa
For purely OO purpose and to help documentation parser, I would like to write something like :
Code: Select all
class abc{
XML $xml;
mailBox $mailBox;
public function run($argv="",$argc="") {}
}
So, basically I would like to declare objects (XML and mailBox) before initialize them in the function.
Why ?
- Because I the program have the data to initialize them yet (the data will be retrieved in the function)
- the programmer and documentation tools have to know that the class has an XML and a mailBox, and that they aren't just object, but object that come from specific class.
Any idea ?
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 5:09 am
by jackpf
Couldn't you just do this?
Code: Select all
class foo
{
private
$xml,
$mailbox,
public function __construct(XML $xml, MailBox $mailbox)
{
$this->xml = $xml;
$this->mailbox = $mailbox;
}
}
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 5:29 am
by aaaaaaaa
Thank you for your answer.
I can do that. But I would prefer to say that the $xml property is an instance of the XML class. In other words, when I use UML reverse engineering tool, it could link the classes together and say "Oh, the firstClass has ``one`` XML object. Let's draw a link between firstClass class and XML class".
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 5:32 am
by aaaaaaaa
I actually tried to do
Code: Select all
class abc{
$xml = new XML();
$mailBox = new mailBox();
public function run($argv="",$argc="") {
$xml = new XML(list of argument);
$mailBox = new mailBox(another list of argument);
}
That would have done the trick, but that actually doesn't work
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 5:39 am
by Weiry
you could use the above example from jackpf, but you would utilize it like:
Code: Select all
$xmlObject = new XML();
$mailboxObject = new MailBox();
$fooClass = new foo($xmlObject, $mailboxObject);
the constructor for foo requires an instance of XML and MailBox in order to construct anyways... you can't construct it without them.
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 7:13 am
by jackpf
aaaaaaaa wrote:I actually tried to do
Code: Select all
class abc{
$xml = new XML();
$mailBox = new mailBox();
public function run($argv="",$argc="") {
$xml = new XML(list of argument);
$mailBox = new mailBox(another list of argument);
}
That would have done the trick, but that actually doesn't work
You can't have any actual executable code outside of methods.
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 7:26 am
by aaaaaaaa
You can't have any actual executable code outside of methods.
BAD ! BAD ! BAD ! What is doing Oracle's Java's influence over other OO languages !?!!
thank you for your answers

Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 8:45 am
by aaaaaaaa
Weiry wrote:You can't have any actual executable code outside of methods.
Well, actually,one can.
Code: Select all
class fooBar {
private $bar="FOO";
protected function barafoo(){}
}
declare $bar and initialize it, outside of a function, in a class.
What I would like to do (or would have liked to do) is to do the same, but instead of creating a string, create an object, in a class, outside a function. It's not possible in PHP, is it ?
Maybe you meant that
is not really a executable code ?
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 10:46 am
by jackpf
Well...what i mean is, you can only define the methods and properties of the class. Yeah, you can assign static variables...but that's just defining a property.
For example....
Code: Select all
class foo
{
public
$var = 'something', // will work
$var = 100, // will work
$var = $_SERVER['PHP_SELF'], // won't work!
$var = some_function(); // won't work!
}
Instead you should put the code that assigns the variables a value in the class constructor.
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 11:38 am
by aaaaaaaa
Thank you Jackpf

have a nice Week End
Re: OO question : declare objects before initialize them ?
Posted: Fri Sep 11, 2009 12:26 pm
by jackpf
No problem
You too mate.