"construct" in/for functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ManWithNoName
    New Member
    • Aug 2007
    • 88

    "construct" in/for functions?

    Hello,

    I am wondering if it is possible to have something like "__construc t()" for functions/methods in PHP?

    e.g.

    Code:
    class x{
    
    function y(){
      echo "foo";
    }
    
    function x(){
      echo "bar";
    }
    
    }
    
    new $x;
    $x->x(); // should automatically also call y() before init x()
    
    with output result as: "foobar"
    Is this possible?

    Where can I find more info?

    Thanks!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    In your x::x() method, you could call the x::y() method.

    Code:
    class x {
        function y() {
            echo 'foo';
        }
        function x() {
            $this->foo();
            echo 'bar';
        }
    }
    Also, PHP5 has a much better object model than PHP4, so check it out.

    Comment

    • ManWithNoName
      New Member
      • Aug 2007
      • 88

      #3
      Hi! thanks for your reply!

      However, that was not exactly what I was thinking about,

      what I am looking for is a way to make sure that method x(), without the need to manually call y() within x(), will access y(),

      that is, the equivalent to doing this (where the entire class "y" is basically representing the method "y" in the previous example):

      Code:
      class y{
      
      function __construct(){
        echo "foo";
      }
      
      function x(){
        echo "bar";
      }
      
      }
      
      $y=new y;
      
      $y->x();// will output "foobar"
      I am not good with "magic" stuff so I was hoping that somebody knew if it is possible to use some form of "magic" to do this (or any other ideas for that matter).

      Oh, I am using php 5.

      thanks!

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Uh, no, as far as I know, that is not possible. I don't understand why you'd want to do that either, but that's neither here nor there.

        You're using PHP4 syntax though. Check out PHP5's model.

        Comment

        • ManWithNoName
          New Member
          • Aug 2007
          • 88

          #5
          If you have a function that all your functions have to run, then it would be rather convenient instead of manually setting the function in all your functions, wouldn't it?

          I am currently looking at the PHP manual (it is one's best friend ;)

          But thanks anyway!

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by ManWithNoName
            If you have a function that all your functions have to run, then it would be rather convenient instead of manually setting the function in all your functions, wouldn't it?

            I am currently looking at the PHP manual (it is one's best friend ;)

            But thanks anyway!
            Well, how would a method know what other methods to call, without you explicitly telling them?

            Mark.

            Comment

            • ManWithNoName
              New Member
              • Aug 2007
              • 88

              #7
              Ps...

              "php4 syntax"? Are you referring to the fact I am not assigning a "ppr" type to my functions? (they are automatically set to public)

              Because I can't see the difference in syntax which you are alluding...

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by ManWithNoName
                "php4 syntax"? Are you referring to the fact I am not assigning a "ppr" type to my functions? (they are automatically set to public)

                Because I can't see the difference in syntax which you are alluding...
                You don't use the public, private, or protected modifiers which are available. No big deal; just more control.

                Comment

                • ManWithNoName
                  New Member
                  • Aug 2007
                  • 88

                  #9
                  Originally posted by Markus
                  Well, how would a method know what other methods to call, without you explicitly telling them?

                  Mark.
                  Yes, that is a correct observation, however, my assumption is based on that all methods in the class (e.g. x,z,q,w... ) are supposed to use the specific method (y)

                  i.e., a method x knows to call method y because it is not method y,

                  is that so unreasonable ;)

                  EDIT

                  To clarify: if one can create or set a constructor for methods,

                  then all methods will call the constructor (when the method is accessed), except the constructor (which shouldn't be accessed; could e.g. be set to private).

                  Comment

                  • code green
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1726

                    #10
                    If I understand correctly, you want every class method to call a common class method without actually calling it.
                    Rather bizarre logic and Atli did hint as to why?
                    I am not aware of any language that can call a function automatically.
                    Can you not make use of class members somehow?

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      is it so devastating to write one(!) line at the beginning of the method code?

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        I'm not sure if this is a good idea in general, but...

                        The __call magic-method could be used for this purpose.

                        It is called whenever an inaccessible method is called. So if you were to make all your method private, the __call method would be triggered each time you tried to call them, which would allow you to do, well, whatever you wanted before calling the methods.

                        Consider this:
                        [code=php]<?php
                        class foo
                        {
                        /** Called whenever an inaccessible (private or non-existing) function is called */
                        function __call($name, $arguments)
                        {
                        if($name != "commonMeth od" && method_exists($ this, $name)) {
                        $this->commonMethod() ;
                        $this->$name($argumen ts);
                        }
                        else {
                        user_error("Cal l to an undefined method '". __CLASS__ ."::{$name}' ", E_USER_ERROR);
                        }
                        }

                        /** To be called before all other methods */
                        private function commonMethod()
                        {
                        echo "foo";
                        }

                        /**
                        * Random test methods
                        * They are private to make them inaccessible, which
                        * will trigger the __call method when they are called.
                        */
                        private function testFunc1()
                        {
                        echo "bar1\n";
                        }
                        private function testFunc2()
                        {
                        echo "bar2\n";
                        }
                        }

                        $inst = new foo();
                        $inst->testFunc1();
                        $inst->testFunc2();
                        ?>[/code]
                        Output:
                        Code:
                        foobar1
                        foobar2
                        Last edited by Atli; Sep 10 '09, 02:07 PM. Reason: Minor tweak to the code.

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          Originally posted by Atli
                          I'm not sure if this is a good idea in general
                          I’d think it is not (in terms of maintainability ). when you do any coding in one of the methods, you may forget that commonMethod() will be called, which may cause odd results (and what, if you need one function, that should not use commonMethod()? ).

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Originally posted by Atli
                            I'm not sure if this is a good idea in general, but...

                            The __call magic-method could be used for this purpose.

                            It is called whenever an inaccessible method is called. So if you were to make all your method private, the __call method would be triggered each time you tried to call them, which would allow you to do, well, whatever you wanted before calling the methods.

                            Consider this:
                            [code=php]<?php
                            class foo
                            {
                            /** Called whenever an inaccessible (private or non-existing) function is called */
                            function __call($name, $arguments)
                            {
                            if($name != "commonMeth od" && method_exists($ this, $name)) {
                            $this->commonMethod() ;
                            $this->$name($argumen ts);
                            }
                            else {
                            user_error("Cal l to an undefined method '". __CLASS__ ."::{$name}' ", E_USER_ERROR);
                            }
                            }

                            /** To be called before all other methods */
                            private function commonMethod()
                            {
                            echo "foo";
                            }

                            /**
                            * Random test methods
                            * They are private to make them inaccessible, which
                            * will trigger the __call method when they are called.
                            */
                            private function testFunc1()
                            {
                            echo "bar1\n";
                            }
                            private function testFunc2()
                            {
                            echo "bar2\n";
                            }
                            }

                            $inst = new foo();
                            $inst->testFunc1();
                            $inst->testFunc2();
                            ?>[/code]
                            Output:
                            Code:
                            foobar1
                            foobar2
                            That's just bad practice, you bad-practice preacher! ;-)

                            Comment

                            • Atli
                              Recognized Expert Expert
                              • Nov 2006
                              • 5062

                              #15
                              I did say it may not be the best of ideas :-]

                              Comment

                              Working...