"construct" in/for functions?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #16
    Originally posted by Dormilich
    (and what, if you need one function, that should not use commonMethod()? ).
    Declare it public. Then __call won't be triggered.

    But yea, I agree. A method such as this could (and probably would) cause minor headaches when in comes to maintaining the code.

    Comment

    • ManWithNoName
      New Member
      • Aug 2007
      • 88

      #17
      Oh my… I thought this thread died out ;)

      Thank you all for your replies and input! :D

      But the cookie goes to “Atli”, his example actually seems to do what I am looking for! :D

      =============== =========

      Yes, the main reason I want to do this is because of convenience ;)

      But there are other uses as well?

      E.g., if someone creates a class that extends the main class then there could be certain methods you as the main class designer want any subset of classes to do regardless if that person intentionally or unintentionally omits your commonMethod().

      I believe the question of maintaining depends on how this is used, just as __construct() in general is used, i.e. it all depends what kind of functions are run in the commonMethod().

      =============== =========
      One last thing, are there any other penalties with using __call()?

      The reason I am not familiar with this (__call) and its family functions (__get,__set etc.) is that I once picked up that these types of functions (overloading?) are not good to use, e.g. because of performance, and that it is not “standard” (other languages do not use these kind of things, and thus it should be avoided, another example is using “isset()” to check if var is set) – don’t ask me to cite any sources, this comes from memory ;)

      (I am currently reading the comment section at php.net to see if this is discussed)

      Once again thanks all! :D

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #18
        Originally posted by ManWithNoName
        E.g., if someone creates a class that extends the main class then there could be certain methods you as the main class designer want any subset of classes to do regardless if that person intentionally or unintentionally omits your commonMethod().
        I as a subdesigner would not be comfortable with such a main designer.

        Originally posted by ManWithNoName
        The reason I am not familiar with this (__call) and its family functions (__get,__set etc.) is that I once picked up that these types of functions (overloading?) are not good to use, e.g. because of performance,
        (overloading in the PHP sense)
        for most applications, you probably won’t notice the performance drop.

        Originally posted by ManWithNoName
        and that it is not “standard” (other languages do not use these kind of things, and thus it should be avoided,
        by the way some other language’s programmers look down at PHP, I don’t care. I can use something they don’t even have.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #19
          Originally posted by ManWithNoName
          Thank you all for your replies and input! :D

          But the cookie goes to “Atli”, his example actually seems to do what I am looking for! :D

          =============== =========

          Yes, the main reason I want to do this is because of convenience ;)

          But there are other uses as well?

          E.g., if someone creates a class that extends the main class then there could be certain methods you as the main class designer want any subset of classes to do regardless if that person intentionally or unintentionally omits your commonMethod().

          I believe the question of maintaining depends on how this is used, just as __construct() in general is used, i.e. it all depends what kind of functions are run in the commonMethod().

          =============== =========
          One last thing, are there any other penalties with using __call()?

          The reason I am not familiar with this (__call) and its family functions (__get,__set etc.) is that I once picked up that these types of functions (overloading?) are not good to use, e.g. because of performance, and that it is not “standard” (other languages do not use these kind of things, and thus it should be avoided, another example is using “isset()” to check if var is set) – don’t ask me to cite any sources, this comes from memory ;)

          (I am currently reading the comment section at php.net to see if this is discussed)

          Once again thanks all! :D
          There are ways to force implementation of methods on any subclasses - make the method (and subsequently, the class) abstract. Then any extending classes are forced to implement that method.

          You could make the method final. That way, any subclasses cannot override your implementation, and you have complete control (you do not have to worry whether the subclass implements your method, because you know you have).

          By the way, these are all available in PHP5. :)

          Mark.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #20
            Originally posted by ManWithNoName
            Thank you all for your replies and input! :D

            But the cookie goes to “Atli”, his example actually seems to do what I am looking for! :D

            =============== =========

            Yes, the main reason I want to do this is because of convenience ;)

            But there are other uses as well?

            E.g., if someone creates a class that extends the main class then there could be certain methods you as the main class designer want any subset of classes to do regardless if that person intentionally or unintentionally omits your commonMethod().

            I believe the question of maintaining depends on how this is used, just as __construct() in general is used, i.e. it all depends what kind of functions are run in the commonMethod().

            =============== =========
            One last thing, are there any other penalties with using __call()?

            The reason I am not familiar with this (__call) and its family functions (__get,__set etc.) is that I once picked up that these types of functions (overloading?) are not good to use, e.g. because of performance, and that it is not “standard” (other languages do not use these kind of things, and thus it should be avoided, another example is using “isset()” to check if var is set) – don’t ask me to cite any sources, this comes from memory ;)

            (I am currently reading the comment section at php.net to see if this is discussed)

            Once again thanks all! :D
            Java uses overloading. C# uses overloading. These are mainstream languages. It's not that it's not 'standard', I just don't think you'be realized the different ways to achieve what you want - see my previous post.

            Also, what's wrong with isset()? How would you not use it? Write your own PHP method? It is in the SPL, which means it is faster than anything you or I could write. Plus, just because PHP makes available things that other languages doesn't, it should not be looked down upon -- anyone who does so is just an arrogant ass -- it provides many ways to do things fast and efficiently - that isn't a 'con' to me.

            Mark.

            Comment

            • ManWithNoName
              New Member
              • Aug 2007
              • 88

              #21
              Thanks for your further input!

              Regarding,

              “by the way some other language’s programmers look down at PHP, I don’t care. I can use something they don’t even have.”

              and,

              “Java uses overloading. C# uses overloading. These are mainstream languages. It's not that it's not 'standard', I just don't think you'be realized the different ways to achieve what you want - see my previous post.”

              and,

              “Also, what's wrong with isset()? How would you not use it? Write your own PHP method? It is in the SPL, which means it is faster than anything you or I could write. Plus, just because PHP makes available things that other languages doesn't, it should not be looked down upon -- anyone who does so is just an arrogant ass -- it provides many ways to do things fast and efficiently - that isn't a 'con' to me.”

              First off, not trying to start a flame ;)

              I believe isset() is a good example regarding “PHP vs. the Rest”. I use it all the time,* put aside performance advantages, I do not understand how other languages can do without it, I tried replacing it with other techniques but they all seemed so cumbersome,

              (* I know I phrased myself somewhat poorly in my last post; I meant: isset() is an example of "non-standard" behaviour, not that I was not using it)

              and how did I ever bother looking for alternatives?

              Yes well, when I was starting to learn Python I quickly notice that the language lacks such implementation, and that isset() was not a proper way of doing things (incidentally, a post about it on bytes.com, http://bytes.com/topic/python/answer...t-exist-python; first result in Google ;) .

              I believe the point is that one should init all the program vars at the beginning of the program and not relay on unknown vars. Anyways, isset(), I believe, is something all languages should have!

              And now on to the main course:

              “There are ways to force implementation of methods on any subclasses - make the method (and subsequently, the class) abstract. Then any extending classes are forced to implement that method. You could make the method final. That way, any subclasses cannot override your implementation, and you have complete control (you do not have to worry whether the subclass implements your method, because you know you have).”

              Thanks for that! I quickly made all my methods in my abstract class final! `-´

              This does not however work for my intended purpose.

              My abstract class contains all the standard methods (set, get, and so on), and my extended classes contains new functionality (e.g. transform XML) that does not belong in the abstract class.

              Now, when a public method is called in the extended class I also need certain other methods (that do reside in the abstract class) to also be called.

              I am not sure this will clarify…

              Code:
              // Abstract class
              function transform(){
                $this->commonMethod();
                // The “concept” transform
              }
              // XML class
              function transform(){
                // transform XML
              }
              Basically, I need to merge does two… Does this make any sense?

              “By the way, these are all available in PHP5. :)”

              Oh gee, I am using php 5, why won’t you believe me ;)

              Comment

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

                #22
                believe the point is that one should init all the program vars at the beginning of the program and not relay on unknown vars. Anyways, isset(), I believe, is something all languages should have!
                Strongly typed languages such as C/C++ C# do not allow use of an undeclared variable so isset() is useless
                Now, when a public method is called in the extended class I also need certain other methods (that do reside in the abstract class) to also be called.
                As Dorm hinted - Well why not call the damn things?
                I suspect you are a self-taught programmer. I do not mean that as an insult, but being self-educated can lead to strange ideas and unconventional styling.
                You should pay heed to the comments that have been posted

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #23
                  Originally posted by ManWithNoName
                  Regarding,

                  “by the way some other language’s programmers look down at PHP, I don’t care. I can use something they don’t even have.”

                  and,

                  “Java uses overloading. C# uses overloading. These are mainstream languages. It's not that it's not 'standard', I just don't think you'be realized the different ways to achieve what you want - see my previous post.”

                  and,

                  “Also, what's wrong with isset()? How would you not use it? Write your own PHP method? It is in the SPL, which means it is faster than anything you or I could write. Plus, just because PHP makes available things that other languages doesn't, it should not be looked down upon -- anyone who does so is just an arrogant ass -- it provides many ways to do things fast and efficiently - that isn't a 'con' to me.”

                  First off, not trying to start a flame ;)

                  I believe isset() is a good example regarding “PHP vs. the Rest”. I use it all the time,* put aside performance advantages, I do not understand how other languages can do without it, I tried replacing it with other techniques but they all seemed so cumbersome,

                  (* I know I phrased myself somewhat poorly in my last post; I meant: isset() is an example of "non-standard" behaviour, not that I was not using it)

                  and how did I ever bother looking for alternatives?

                  Yes well, when I was starting to learn Python I quickly notice that the language lacks such implementation, and that isset() was not a proper way of doing things (incidentally, a post about it on bytes.com, http://bytes.com/topic/python/answer...t-exist-python; first result in Google ;) .

                  I believe the point is that one should init all the program vars at the beginning of the program and not relay on unknown vars. Anyways, isset(), I believe, is something all languages should have!

                  And now on to the main course:

                  “There are ways to force implementation of methods on any subclasses - make the method (and subsequently, the class) abstract. Then any extending classes are forced to implement that method. You could make the method final. That way, any subclasses cannot override your implementation, and you have complete control (you do not have to worry whether the subclass implements your method, because you know you have).”

                  Thanks for that! I quickly made all my methods in my abstract class final! `-´

                  This does not however work for my intended purpose.

                  My abstract class contains all the standard methods (set, get, and so on), and my extended classes contains new functionality (e.g. transform XML) that does not belong in the abstract class.

                  Now, when a public method is called in the extended class I also need certain other methods (that do reside in the abstract class) to also be called.

                  I am not sure this will clarify…

                  Code:
                  // Abstract class
                  function transform(){
                    $this->commonMethod();
                    // The “concept” transform
                  }
                  // XML class
                  function transform(){
                    // transform XML
                  }
                  Basically, I need to merge does two… Does this make any sense?

                  “By the way, these are all available in PHP5. :)”

                  Oh gee, I am using php 5, why won’t you believe me ;)
                  Regarding: I believe the point is that one should init all the program vars at the beginning of the program and not relay on unknown vars. Anyways, isset(), I believe, is something all languages should have!

                  I disagree that all variables should be defined before use - you cannot know for certain that a $_GET, $_POST, etc., variable exists, and you cannot declare it yourself, lest you overwrite the original; that is where isset() comes in useful.

                  Regarding: I am not sure this will clarify…

                  You're right - it won't. I'm not following your problem anymore :P

                  P.S. I do believe you're using PHP5 - I was just saying!
                  P.P.S. No flames wars, just healthy discussion.

                  Comment

                  • ManWithNoName
                    New Member
                    • Aug 2007
                    • 88

                    #24
                    Originally posted by code green
                    Strongly typed languages such as C/C++ C# do not allow use of an undeclared variable so isset() is useless
                    They could probably work something out.

                    As Dorm hinted - Well why not call the damn things?
                    That is the obvious solution, yes, but I was looking for an alternative way of doing this...

                    You should pay heed to the comments that have been posted
                    ... Which Atli did provide.

                    I suspect you are a self-taught programmer. I do not mean that as an insult, but being self-educated can lead to strange ideas and unconventional styling.
                    Obviously you are an experience and indoctrinated programmer. I do not mean that as an insult, but sometimes old conventions need to be challenged.

                    ///

                    You're right - it won't. I'm not following your problem anymore :P
                    Well, thanks for the time you did put down to help me,

                    and the same goes to the rest of you, you have all provided me with more than enough to solve my problem.

                    Thank you.

                    Comment

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

                      #25
                      Well, thanks for the time you did put down to help me,
                      Just as the post was getting interesting

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #26
                        Originally posted by code green
                        Just as the post was getting interesting
                        yea, I wish there were more threads as interesting as this one.

                        Comment

                        Working...