object default value in constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Janez Cerar

    object default value in constructor

    I'm trying to write a class with a constructor that takes a single object
    parameter. So far so good, but I can't for the life of me make the parameter
    optional. I understand how to do it with a numerical or textual parameter,
    but not with an object. I tried null, but it doesn't work.

    class A
    {
    }

    class B
    {
    function B($a = ???)
    {
    if (!isset($a)) $a = new A;
    }
    }

    Any ideas?

    Jaka


  • Nikolai Chuvakhin

    #2
    Re: object default value in constructor

    "Janez Cerar" <janez.cerar@em ail.removethis. si> wrote
    in message news:<9KTlb.485 2$2B6.896815@ne ws.siol.net>...[color=blue]
    >
    > I'm trying to write a class with a constructor that takes a single object
    > parameter.[/color]

    Why? Isn't it easier to just extend the class?
    [color=blue]
    > So far so good, but I can't for the life of me make the parameter
    > optional.
    >
    > class A
    > {
    > }
    >
    > class B
    > {
    > function B($a = ???)
    > {
    > if (!isset($a)) $a = new A;
    > }
    > }[/color]

    Try this instead:

    class A {
    function A () {}
    }
    class B extends A {}

    Since you defined no constructor for B, new B will simply call the
    constructor for A (in PHP 4, at least).

    Cheers,
    NC

    Comment

    • Phil Roberts

      #3
      Re: object default value in constructor

      With total disregard for any kind of safety measures nc@iname.com
      (Nikolai Chuvakhin) leapt forth and uttered:
      [color=blue]
      > Since you defined no constructor for B, new B will simply call
      > the constructor for A (in PHP 4, at least).[/color]

      Not strictly true. The parent constructor is not called by default,
      it has be called explicitly.

      class ClassOne {
      var $number;
      function ClassOne() {
      $this->number = 2;
      }
      }

      class ClassTwo extends ClassOne {
      function ClassTwo() {
      # Call the parent constructor
      parent::ClassOn e();
      }

      function classMethod() {
      return $this->number;
      }
      }

      $obj =& new ClassTwo;

      # Will echo '2';
      echo $obj->classMethod( );

      --
      There is no signature.....

      Comment

      • André Næss

        #4
        Re: object default value in constructor

        Janez Cerar:
        [color=blue]
        > I'm trying to write a class with a constructor that takes a single object
        > parameter. So far so good, but I can't for the life of me make the
        > parameter optional. I understand how to do it with a numerical or textual
        > parameter, but not with an object. I tried null, but it doesn't work.
        >
        > class A
        > {
        > }
        >
        > class B
        > {[/color]
        function B($a = null)[color=blue]
        > {[/color]
        if(!$a) { $a = new A(); }[color=blue]
        > }
        > }[/color]

        isset() checks if a variable is set or not. A variable which is set to null
        is of course set. null is equivalent to false.

        André Næss

        Comment

        • Nikolai Chuvakhin

          #5
          Re: object default value in constructor

          Phil Roberts <philrob@HOLYfl atnetSHIT.net> wrote
          in message news:<Xns941ECB 5CAAA26philrobe rts@206.127.4.2 2>...[color=blue]
          >
          > With total disregard for any kind of safety measures nc@iname.com
          > (Nikolai Chuvakhin) leapt forth and uttered:
          >[color=green]
          > > Since you defined no constructor for B, new B will simply call
          > > the constructor for A (in PHP 4, at least).[/color]
          >
          > Not strictly true. The parent constructor is not called by default,
          > it has be called explicitly.[/color]

          The parent constructor IS called by default, if the child class
          has no constructor defined:

          ...in PHP 4... If a class has no constructor, the constructor
          of the base class is being called, if it exists.

          PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


          This is really easy to test:

          class A {
          var $one;
          var $two;
          function A () {
          $this->one = 'String One';
          $this->two = 'Sting Two';
          }
          }

          class B extends A {
          function show () {
          echo 'Field one = ', $this->one, "\r\n";
          echo 'Field two = ', $this->two, "\r\n";
          }
          }

          $b = new B();
          $b->show();

          The above code snippet prints:

          Field one = String One
          Field two = Sting Two

          Cheers,
          NC

          Comment

          Working...