In OOP, the concept of inheritance allows a class to accept or have the properties of another class (inherited). A sub-class or derived class is a class that inherits the characteristics/methods of another class. Heritage reduces code redundancy, allowing the class to use other class methods without having to rewrite the code. This allows for dynamic programming using the main class’s methods (also called the parent class). A subclass has the same properties as a primary class, but it can also have additional methods that are either completely independent or not present in the main class.
What is the Function of Inheritance?
Children’s classes may be built to enhance the parent’s class functionality and add more characteristics once essential properties and behaviors have been defined in the parent’s class.
This distinction is demonstrated by creating a parent class child class(HerdingDog) (Dog) and then adding a single herd() behaviour.
The following code shows how the child class (HerdingDog) inherits a parent class method (bark) (Dog), and the child class adds an extra method herd().
When the code uses the fluffy.bark() function, the method bark() travels up the child’s chain to find out where the bark method is declared.
Computer programming skills is in high demand in today’s technology culture for programming. Understanding object-oriented programming may be useful in the creation and management of software systems (OOP). This article covers the core concepts of OOP and illustrates them with simple examples.
What is Object-oriented programming (OOP)?
Object-oriented programming is the process of integrating variables (properties) and functions (methods) into a single object. These products are organized into classes that may be mixed and matched. OOP allows you to think about the objects in a program’s code as well as the various actions that go along with them.
What are the principles of Object-oriented programming (OOP)?
Encapsulation, Abstraction, Inheritance, and Polymorphism are the four basic concepts of object-oriented programming. Although these concepts appear to be extremely complicated, understanding the broad framework of how they work is beneficial. The four basic hypotheses are as follows:
Inheritance: child classes inherit data and behaviors from parent class
Encapsulation: containing information in an object, exposing only selected information
Abstraction: only exposing high level public methods for accessing an object
$_GET is a global variable that is used to collect data from an HTML form after it has been submitted. The data is visible in the query string when the form uses method get to transmit data, thus the values are not concealed. The values in the URL are stored in the $_GET super global array variable.
Right now, we’re just seeing half of the rationale. We’ve generated a linked image of Nainital Lake in the preceding code, which will take us to the picture.php page and provide the parameters name=”Nainilake” and city=”Nainital” to it.
That is, if we click on the little image of Nainital Lake, we will be brought to the next page picture.php, where the parameters will be displayed. These arguments will be given to the next page using the get method, and they will be shown in the address bar, as get is the default way. When we wish to transmit data to an address, we use a question mark (?) to connect them to the address.
The address has the name=Nainilake parameter connected to it. If we wish to add more values, we may do so by appending an ampersand (&) after each key-value combination, similar to how city=Nainital is appended after the name parameter. Now, when you click on the image of Nainital Lake, we want the picture.php page to appear, along with the value of the parameter.
It’s a global variable that’s used to collect data from the HTML form after it’s been submitted. Because the data is not accessible in the query string when a form uses method post to transfer data, security standards are maintained in this manner.
Example:- A form with an input field and a submit button is shown in the example below. The form data is delivered to the file provided in the action element of the form> tag when a user submits the data by clicking “Submit.” For processing form data, we point to the file itself in this case. Replace it with the filename of your choosing if you want to process form data with another PHP file. The value of the input field may then be collected using the super global variable $_POST:
We have developed a form in the preceding code that accepts the user’s name and age and accesses the data using the $_POST super global variable when they submit the data. Each super global variable is an array, thus it may hold many values. As a result, we took the $_POST variable’s name and age and placed them in the $nm and $age variables. We have developed a form in the preceding code that accepts the user’s name and age and accesses the data using the $_POST super global variable when they submit the data. Each super global variable is an array, thus it may hold many values. As a result, we took the $_POST variable’s name and age and placed them in the $nm and $age variables.
$_REQUEST is a super global variable that’s used to collect data when an HTML form is submitted. Because $_POST and $_GET accomplish the same purpose and are extensively used, $_REQUEST is rarely used.
Example:- A form with an input field and a submit button is shown in the example below. The form data is delivered to the file provided in the action element of the form> tag when a user submits the data by clicking “Submit.” For processing form data, we point to this file in our example. Replace it with the filename of your choosing if you want to process form data with another PHP file. The value of the input field may then be collected using the super global variable $_REQUEST:
We’ve constructed a form in the above code that accepts the user’s name as input and prints it when the submit button is pressed. Because we alter the data on the same page using PHP code, we transfer the data received in the form to the same page using the $_SERVER[‘PHP SELF’] element as stated in the action attribute. The $_REQUEST super global array variable is used to obtain the data.
It’s a super global variable in PHP that keeps track of headers, paths, and script locations. Some of these components are used to obtain data from the $_SERVER super global variables.
To retrieve some information, we used the $_SERVER components in the code above. Using the ‘PHP SELF’ element, we retrieve the current file name that is being worked on. Then, using the ‘SERVER NAME’ attribute, we retrieve the current server name. Then we use ‘HTTP HOST’ to retrieve the host name.
The following table lists the most important elements that can go inside $_SERVER:
Returns the filename of the currently executing script
Returns the version of the Common Gateway Interface (CGI) the server is using
Returns the IP address of the host server
Returns the name of the host server (such as www.w3schools.com)
Returns the server identification string (such as Apache/2.2.24)
Returns the name and revision of the information protocol (such as HTTP/1.1)
Returns the request method used to access the page (such as POST)
Returns the timestamp of the start of the request (such as 1377687496)
Returns the query string if the page is accessed via a query string
Returns the Accept header from the current request
Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1)
Returns the Host header from the current request
Returns the complete URL of the current page (not reliable because not all user-agents support it)
Is the script queried through a secure HTTP protocol
Returns the IP address from where the user is viewing the current page
Returns the Host name from where the user is viewing the current page
Returns the port being used on the user’s machine to communicate with the web server
Returns the absolute pathname of the currently executing script
Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as firstname.lastname@example.org)
Returns the port on the server machine being used by the web server for communication (such as 80)
Returns the server version and virtual host name which are added to server-generated pages
Returns the file system based path to the current script
$GLOBALS is a super global variable that may be accessed from anywhere in the PHP script. All global variables are stored in the array $GLOBALS, where index is the global variable name that may be retrieved.
All global variables are stored in an array named $GLOBALS[index] in PHP. The variable’s name is stored in the index.
The example below shows how to use the super global variable $GLOBALS:
Two global variables, $x and $y, are declared in the above code and are given a value. Then, in the GLOBAL array, a function multiplication() is written to multiply the values of $x and $y and store the result in another variable $z.
These are PHP array variables with specific definitions that make it simple to retrieve information about a request or its context. The super global variables may be found all across your script. These variables can be accessible from any function, class, or file without the need to do any further steps, such as defining a global variable. They are mostly employed in applications to save and retrieve data from one page to the next.
This function is used to read a single line from a file. You can pass this function a file handle corresponding to an open file, and optional length.
Why fgets() used in Reading file?
It returns a line from a file pointer and terminates at a given length, end of file(EOF), or new line, whichever comes first.
The fgets() function is passed the file to read and the number of bytes to read as arguments, and it returns a string of length -1 bytes from the file indicated by the user.
If it fails, it returns False.
The fgets() function in PHP accepts two parameters.
file : It specifies the file from which characters have to be extracted.
length : It specifies the number of bytes to be read by the fgets() function. The default value is 1024 bytes.
Return Value : It returns a string of length -1 bytes from the user-specified file, or False if the operation fails.
Errors And Exceptions:-
Because it reads a single line at a time, the method isn’t designed for huge files, and reading a long file can take a long time.
If the fgets() method is called more than once, the buffer must be emptied.
Although the fgets() method returns the Boolean False, it frequently produces a non-Boolean result that evaluates to False.
Let’s take one Example:-
Suppose there is a file named “test.txt” which consists of :
This is the first line.
This is the second line.
This is the third line.
PHP has a number of functions for reading data from files. You may read entire file data, read data line by line, or read data character by character using various methods.
How many types of PHP file Read Functions are:-
The available PHP file read functions are given below.
PHP readfile() Function
Reads a file and publishes it to the output buffer with the readfile() method.
Assume we have a text file on the server named “webdictionary.txt” that looks like this:
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language
The following is the PHP code for reading the file and writing it to the output buffer (the readfile() method returns the number of bytes read if successful):
If all you want to do is open a file and read its contents, the readfile() method comes in handy.