"construct" in/for functions?
Collapse
X
-
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! :DComment
-
for most applications, you probably won’t notice the performance drop.
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
-
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
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
-
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
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
-
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 }
“By the way, these are all available in PHP5. :)”
Oh gee, I am using php 5, why won’t you believe me ;)Comment
-
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!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 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 postedComment
-
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 }
“By the way, these are all available in PHP5. :)”
Oh gee, I am using php 5, why won’t you believe me ;)
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
-
As Dorm hinted - Well why not call the damn things?
You should pay heed to the comments that have been posted
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're right - it won't. I'm not following your problem anymore :P
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
-
Comment
Comment