OO question : declare objects before initialize them ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

OO question : declare objects before initialize them ?

Post 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 ?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: OO question : declare objects before initialize them ?

Post 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;
    }
}
 
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

Re: OO question : declare objects before initialize them ?

Post 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".
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

Re: OO question : declare objects before initialize them ?

Post 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
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: OO question : declare objects before initialize them ?

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: OO question : declare objects before initialize them ?

Post 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.
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

Re: OO question : declare objects before initialize them ?

Post 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 :)
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

Re: OO question : declare objects before initialize them ?

Post 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

Code: Select all

private $bar="FOO";
is not really a executable code ?
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: OO question : declare objects before initialize them ?

Post 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.
aaaaaaaa
Forum Newbie
Posts: 13
Joined: Wed Aug 19, 2009 8:43 am

Re: OO question : declare objects before initialize them ?

Post by aaaaaaaa »

Thank you Jackpf :D have a nice Week End
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: OO question : declare objects before initialize them ?

Post by jackpf »

No problem :)

You too mate.
Post Reply