Jump to content

Abstracting classes


Go to solution Solved by eldan88,

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/282023-abstracting-classes/
Share on other sites

 

 

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();
Link to comment
https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448909
Share on other sites

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 by kicken
Link to comment
https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448914
Share on other sites

 

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.
Link to comment
https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1448919
Share on other sites

  • Solution

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!!!!

Link to comment
https://forums.phpfreaks.com/topic/282023-abstracting-classes/#findComment-1449053
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.