factory pattern in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael G

    factory pattern in php

    I want to use a factory pattern to create a variety of objects. Which object
    gets created depends on a string that is passed to the factory create
    method. I would like to avoid using an if or switch construct to accomplish
    this but not sure how. For example,

    ....

    if($nextObject == "login")
    {
    return new Login();
    }
    else if($nextObject == "validateda ta")
    {
    return new Validate(aStrin g);
    }

    ....
    Is there any way to avoid this?

    Thanks, Mike



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  • Michael G

    #2
    Re: factory pattern in php


    "Michael G" <mike-g@montana.com> wrote in message
    news:42e66b59$1 _2@spool9-west.superfeed. net...[color=blue]
    >I want to use a factory pattern to create a variety of objects. Which
    >object gets created depends on a string that is passed to the factory
    >create method. I would like to avoid using an if or switch construct to
    >accomplish this but not sure how. For example,
    >
    > ...
    >
    > if($nextObject == "login")
    > {
    > return new Login();
    > }
    > else if($nextObject == "validateda ta")
    > {
    > return new Validate(aStrin g);
    > }
    >
    > ...
    > Is there any way to avoid this?
    >[/color]


    Actually there is a way to do this but I am sure how appropriate it is.
    Here is a simple of example of the concept. It does what I want but not
    convinced this is the best way to do it. If it is then I could simply place
    a list of strings in an array and index them with an input string.

    <?php

    class A
    {
    function A()
    {
    print("creating A");
    }

    }


    $a = "A";
    new $a."()";

    ?>

    Thanks mike



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    • Jacob Atzen

      #3
      Re: factory pattern in php

      On 2005-07-26, Michael G <mike-g@montana.com> wrote:[color=blue]
      > I want to use a factory pattern to create a variety of objects. Which object
      > gets created depends on a string that is passed to the factory create
      > method. I would like to avoid using an if or switch construct to accomplish
      > this but not sure how. For example,
      >
      > ...
      >
      > if($nextObject == "login")
      > {
      > return new Login();
      > }
      > else if($nextObject == "validateda ta")
      > {
      > return new Validate(aStrin g);
      > }
      >
      > ...
      > Is there any way to avoid this?[/color]

      You could do:

      function create($classNa me) {
      return new $className;
      }

      Not sure if it's a better solution though.

      --
      Cheers,
      - Jacob Atzen

      Comment

      • Michael G

        #4
        Re: factory pattern in php


        "Jacob Atzen" <jacob@aub.dk > wrote in message
        news:slrndecvlr .a1s.jacob@tank .aub.dk...[color=blue]
        > On 2005-07-26, Michael G <mike-g@montana.com> wrote:[color=green]
        >> I want to use a factory pattern to create a variety of objects. Which
        >> object
        >> gets created depends on a string that is passed to the factory create
        >> method. I would like to avoid using an if or switch construct to
        >> accomplish
        >> this but not sure how. For example,
        >>
        >> ...
        >>
        >> if($nextObject == "login")
        >> {
        >> return new Login();
        >> }
        >> else if($nextObject == "validateda ta")
        >> {
        >> return new Validate(aStrin g);
        >> }
        >>
        >> ...
        >> Is there any way to avoid this?[/color]
        >
        > You could do:
        >
        > function create($classNa me) {
        > return new $className;
        > }
        >
        > Not sure if it's a better solution though.
        >[/color]

        That's not a bad idea. It would eliminate the need for an array of strings.

        Thanks, Mike



        ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

        Comment

        • Michael G

          #5
          Re: factory pattern in php


          "Michael G" <mike-g@montana.com> wrote in message
          news:42e6a0f3$1 _1@spool9-west.superfeed. net...[color=blue]
          >
          > "Jacob Atzen" <jacob@aub.dk > wrote in message
          > news:slrndecvlr .a1s.jacob@tank .aub.dk...[color=green]
          >> On 2005-07-26, Michael G <mike-g@montana.com> wrote:[color=darkred]
          >>> I want to use a factory pattern to create a variety of objects. Which
          >>> object
          >>> gets created depends on a string that is passed to the factory create
          >>> method. I would like to avoid using an if or switch construct to
          >>> accomplish
          >>> this but not sure how. For example,
          >>>
          >>> ...
          >>>
          >>> if($nextObject == "login")
          >>> {
          >>> return new Login();
          >>> }
          >>> else if($nextObject == "validateda ta")
          >>> {
          >>> return new Validate(aStrin g);
          >>> }
          >>>
          >>> ...
          >>> Is there any way to avoid this?[/color]
          >>
          >> You could do:
          >>
          >> function create($classNa me) {
          >> return new $className;
          >> }
          >>
          >> Not sure if it's a better solution though.
          >>[/color]
          >
          > That's not a bad idea. It would eliminate the need for an array of
          > strings.[/color]

          Just a note, some languages would not allow that. That makes me a bit
          uncomfortable. They would require an explicit open/closed paran at a
          minimum.

          Mike



          ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

          Comment

          • Andy Hassall

            #6
            Re: factory pattern in php

            On Tue, 26 Jul 2005 14:43:36 -0600, "Michael G" <mike-g@montana.com> wrote:
            [color=blue][color=green][color=darkred]
            >>> function create($classNa me) {
            >>> return new $className;
            >>> }
            >>>
            >>> Not sure if it's a better solution though.
            >>>[/color]
            >>
            >> That's not a bad idea. It would eliminate the need for an array of
            >> strings.[/color]
            >
            >Just a note, some languages would not allow that. That makes me a bit
            >uncomfortabl e. They would require an explicit open/closed paran at a
            >minimum.[/color]

            But you're not writing in other languages, you're writing in PHP where it's
            valid syntax, so where's the problem?

            --
            Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
            <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

            Comment

            • Michael G

              #7
              Re: factory pattern in php


              "Andy Hassall" <andy@andyh.co. uk> wrote in message
              news:jvbde11ijf rgk5rmjkp5mb9d6 k2evg1n80@4ax.c om...[color=blue]
              > On Tue, 26 Jul 2005 14:43:36 -0600, "Michael G" <mike-g@montana.com>
              > wrote:
              >[color=green][color=darkred]
              >>>> function create($classNa me) {
              >>>> return new $className;
              >>>> }
              >>>>
              >>>> Not sure if it's a better solution though.
              >>>>
              >>>
              >>> That's not a bad idea. It would eliminate the need for an array of
              >>> strings.[/color]
              >>
              >>Just a note, some languages would not allow that. That makes me a bit
              >>uncomfortable . They would require an explicit open/closed paran at a
              >>minimum.[/color]
              >
              > But you're not writing in other languages, you're writing in PHP where
              > it's
              > valid syntax, so where's the problem?
              >[/color]

              Between my ears!!

              Just because the php interpreter doesn't barf doesn't necessarliy mean that
              it is defined behavior. I have ran across this before with different
              implementations of compilers, undefined behavior that is.

              Thanks, Mike



              ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

              Comment

              • Andy Hassall

                #8
                Re: factory pattern in php

                On Tue, 26 Jul 2005 16:03:28 -0600, "Michael G" <mike-g@montana.com> wrote:
                [color=blue][color=green]
                >> But you're not writing in other languages, you're writing in PHP where
                >> it's
                >> valid syntax, so where's the problem?[/color]
                >
                >Between my ears!!
                >
                >Just because the php interpreter doesn't barf doesn't necessarliy mean that
                >it is defined behavior. I have ran across this before with different
                >implementation s of compilers, undefined behavior that is.[/color]

                It's valid in Perl, and PHP's roots are in Perl :-)

                The Objects page in the manual has an example of creating an object with just:

                $cart = new Cart;

                ... no parens.

                The definition of new (T_NEW) in Zend/zend_language_p arser.y:425 (in PHP
                4.4.0's source) looks to me like it accepts a variable - so "new $className" is
                fine.

                --
                Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
                <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

                Comment

                • Michael G

                  #9
                  Re: factory pattern in php


                  "Andy Hassall" <andy@andyh.co. uk> wrote in message
                  news:g0dde11uu4 j8d9jrgdh1onv5v t8dj8raf3@4ax.c om...[color=blue]
                  > On Tue, 26 Jul 2005 16:03:28 -0600, "Michael G" <mike-g@montana.com>
                  > wrote:
                  >[color=green][color=darkred]
                  >>> But you're not writing in other languages, you're writing in PHP where
                  >>> it's
                  >>> valid syntax, so where's the problem?[/color]
                  >>
                  >>Between my ears!!
                  >>
                  >>Just because the php interpreter doesn't barf doesn't necessarliy mean
                  >>that
                  >>it is defined behavior. I have ran across this before with different
                  >>implementatio ns of compilers, undefined behavior that is.[/color]
                  >
                  > It's valid in Perl, and PHP's roots are in Perl :-)
                  >
                  > The Objects page in the manual has an example of creating an object with
                  > just:
                  >
                  > $cart = new Cart;
                  >
                  > ... no parens.
                  >
                  > The definition of new (T_NEW) in Zend/zend_language_p arser.y:425 (in PHP
                  > 4.4.0's source) looks to me like it accepts a variable - so "new
                  > $className" is
                  > fine.[/color]


                  ok. thanks. but if you start trying to add parameters using concatenation
                  then the interpreter doesn't like that.

                  for example:

                  new $a."(".$paren." )" //this does not work

                  Mike



                  ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                  http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                  ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                  Comment

                  • Michael G

                    #10
                    Re: factory pattern in php


                    "Michael G" <mike-g@montana.com> wrote in message
                    news:42e6bfdb$1 _2@spool9-west.superfeed. net...[color=blue]
                    > new $a."(".$paren." )" //this does not work
                    >[/color]

                    this does work:

                    new $a($paren);

                    That seems so non-intuitive to me. Anyway, just not used to using a language
                    like this. I must say being able to use variables in this way really
                    increases the flexibility of the language. Not that a person needs to use
                    syntax like this alot but at least it is available.

                    On another note, since constructors are simply functions with the same name
                    as the enclosing class, a typical function could be called like this:

                    $a = "foo";

                    $a();

                    The result is very similar to using pointers to functions.

                    Mike




                    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                    Comment

                    • Justin Koivisto

                      #11
                      Re: factory pattern in php

                      Michael G wrote:
                      [color=blue]
                      > "Michael G" <mike-g@montana.com> wrote in message
                      > news:42e6a0f3$1 _1@spool9-west.superfeed. net...
                      >[color=green]
                      >>"Jacob Atzen" <jacob@aub.dk > wrote in message
                      >>news:slrndecv lr.a1s.jacob@ta nk.aub.dk...
                      >>[color=darkred]
                      >>>On 2005-07-26, Michael G <mike-g@montana.com> wrote:
                      >>>
                      >>>>I want to use a factory pattern to create a variety of objects. Which
                      >>>>object
                      >>>>gets created depends on a string that is passed to the factory create
                      >>>>method. I would like to avoid using an if or switch construct to
                      >>>>accomplis h
                      >>>>this but not sure how. For example,
                      >>>>
                      >>>>...
                      >>>>
                      >>>>if($nextObj ect == "login")
                      >>>>{
                      >>>> return new Login();
                      >>>>}
                      >>>>else if($nextObject == "validateda ta")
                      >>>>{
                      >>>> return new Validate(aStrin g);
                      >>>>}
                      >>>>
                      >>>>...
                      >>>>Is there any way to avoid this?
                      >>>
                      >>>You could do:
                      >>>
                      >>>function create($classNa me) {
                      >>> return new $className;
                      >>>}
                      >>>
                      >>>Not sure if it's a better solution though.
                      >>>[/color]
                      >>
                      >>That's not a bad idea. It would eliminate the need for an array of
                      >>strings.[/color]
                      >
                      > Just a note, some languages would not allow that. That makes me a bit
                      > uncomfortable. They would require an explicit open/closed paran at a
                      > minimum.[/color]

                      This is what you be considered a variable function. Best practice is to
                      include the parenthesis like:

                      $className='myC lass';
                      $a = new $className();

                      --
                      Justin Koivisto, ZCE - justin@koivi.co m

                      Comment

                      • Jacob Atzen

                        #12
                        Re: factory pattern in php

                        On 2005-07-27, Justin Koivisto <justin@koivi.c om> wrote:[color=blue]
                        > This is what you be considered a variable function. Best practice is to
                        > include the parenthesis like:[/color]

                        Why is this best practice? I can't seem to conjure any argument as to
                        why this should be better.

                        --
                        Cheers,
                        - Jacob Atzen

                        Comment

                        Working...