eldan88 Posted September 9, 2013 Share Posted September 9, 2013 Hey guys. I am learning about abstracting classes, and I am little bit confused on what the following documentation says on php.net. "Methods defined as abstract simply declare the method's signature - they cannot define the implementation." I would also like to know exactly what is Abstracting, and what are the advantages? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/282023-abstracting-classes/ Share on other sites More sharing options...
trq Posted September 9, 2013 Share Posted September 9, 2013 Methods defined as abstract simply declare the method's signature - they cannot define the implementation. Means that the function cannot do not have a body, they don't actually implement the functionality. eg; abstract protected function exampleFunction(); Quote Link to comment https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448909 Share on other sites More sharing options...
kicken Posted September 9, 2013 Share Posted September 9, 2013 (edited) I would also like to know exactly what is Abstracting, and what are the advantages? Thanks! Read through this "Why Interfaces?" thread on devshed for some details about both interfaces and abstract classes. Edited September 9, 2013 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448914 Share on other sites More sharing options...
.josh Posted September 10, 2013 Share Posted September 10, 2013 Okay I'll give you an example with a script I wrote for something at my job. Basically the purpose of the script is to receive .csv files from some place, parse the data, generate another .csv file based on some defined rules, and send the data back. Well the script itself has lots of moving parts, but the core of it is a while loop that loop. Read a line from the input file, process the line, output the new line to the new file. And within this loop there are two key functions that are run: a function that decides whether or not the input row should be processed, and a function that actually defines the rules for generating the data for the output file. Now here a point in which the script is broken off into a child class. Those two functions are defined in a child class, because from a bigger picture, a user can setup multiple "processes" that receive their own files and have their own rules defined. So the core of that part of the script - the while loop - is the same, but those 2 functions differ from process to process. So what happens if those functions are called and they don't exist? The "core" or "engine" breaks down. So that's where abstract declaration steps in. I can declare abstract public function isValid(); abstract public function generateRow(); in my parent class, and then when someone wants to make a new process, they must define those functions or else the script won't run. The php engine will yell and scream at them and refuse to go any further until they define it. Now, they can do whatever they want inside that function, as long as its defined. It is basically forced acknowledgement that my parent class requires those functions in order to run. Sure, I could put into my parent class a way to check if the function exists, but that's sort of the point: making it easier to ensure that they exist. Quote Link to comment https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448919 Share on other sites More sharing options...
Solution eldan88 Posted September 11, 2013 Author Solution Share Posted September 11, 2013 Thanks a lot guys. I went through your posts and now understand how abstracts are used as templates for child classes, and how they work. @kicken I never knew what interfaces where until I read the forum you sent me. Thank you!!!! Quote Link to comment https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1449053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.