Useful Tips for PHP Developers

To work in PHP firstly understands the concept of (OOP) Object-Oriented Programming (PHP OOP),  is a type of programming language added to php5, that helps in building complex, reusable web applications.

 OOP is easy, simple and fast and easily debug, Once you figure out the basic principles of oops, it will help you to  work faster by using more logical code. There is some concept of OOP are:

Object Oriented Concepts

Let's define important terms related to Object Oriented Programming.

  • Class − Class is a data type in PHP ,which includes local data and functions. We can make a instances of the same kind (or class) of the object.

  • Object − Object is an instance of the class. A Class is defined only once and then make many objects that belong to it.

  • Member function − Member function are those functions which are defined inside a class which are used to access an object data.

  • Member Variable − Variable is called attributes of the object once an object is created we can make a variable and defined inside a class. The data are invisible outside the class and it can be a accessed via member function only.

  • Inheritance − When a class is defined by inheriting by using some existing function from its parent class it is called inheritance.In Heritance child class will inherit some or all functions of their  parent class including their own functions.

  • Parent class − A class that has their own property and methods .This is also called a base class or super class.

  • Child Class − A class that inherits from another class, including their own property. This is also called a subclass or derived class.

  • Polymorphism – Polymorphism is a long word with a very simple concept.Poly means many Morph means form.In polymorphism class have different functionality while sharing a common interface

For example, function name will remain same but it make take different number of arguments and can do different tasks.

  • Overloading − Like a  Polymorphism  Overloading is a functions  have different parameters with similar signatures,when parent class has defined a method and derived class wishes to override that method

  • Data Abstraction − Abstraction is used for hiding the unwanted data and giving relevant data. It solves the problem in the design level.

  • Encapsulation − Encapsulation  is used to bind the data in capsule form.Encapsulation means hiding the code and data into a single unit to protect the data from the outside world.

  • Constructor − refers to a special type of function which will be called automatically  when an object will be  initialized..

  • Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

 

POST and GET in PHP

POST and GET is superglobals, which means that they are always accessible, regardless of scope – Both are used to create an array.

          $_GET is used to pass to the current script via the URL parameters. information sent from a form with the GET method is visible to everyone and the limitation is about 2000 characters.

          $_POST is used passed to the current script via the HTTP POST method.POST has no limits on the amount of information to send. The variables are not displayed in the URL, it is not possible to bookmark the page. Post method is a safe method that’s why developers prefer POST for sending form data.

Database Security

The best and safe way to secure database is always uses the PDO prepared statements to prevent SQL injection from any user input, and I use html special chars to escape before outputting.Your session data is to encrypt the information stored in the session. This does not solve the problem completely since the encrypted data is not completely safe, but at least the information is not readable. PHP provides a method called session_set_save_handler() which can be used to persist data in session in your own way.

File Handling

PHP supports file handling which is used to read, write and append data to the file.

File handling function includes :

  • File opening

  • File Reading

  • File Writing

  • File Closing

File opening

File opening fopen() is a function in php.It is used to open a file in php.File name and opening file are the two arguments in PHP. 

Sr.No

Mode & Purpose of files

1

r

 is used for reading only.

pointer  is  places at the beginning of the file.

2

r+

is used for reading and writing.

Pointer  is  places at the beginning of the file.

3

w

is used  for writing only.

Pointer  is placed at the beginning of the file and Clear all the content the file to zero length. If file is not exist create a new file .

4

w+

is used for reading and writing only.

Pointer  is placed at the beginning of the file.

and Clear all the content the file to zero length. If file is not exist create a new file.

5

a

is used for writing only.

 pointer is placed  at the end of the file.

If file is not exist create a new file.

6

a+

 is used  for reading and writing only.

pointer is placed  at the end of the file.

If file is not exist create a new file.

 

File Reading

Once a file is open using the fopen() function you can read the file using  fread() function .fread() function has two arguments The file pointer and the length of a file .

We can calculate the file size using the  filesize() function .The filesize() takes the file name and its argument and return the size of the file in bytes.

File Writing

fwrite()function is writing a text in a new file or add a new data in an existing file.fwrite() function has two arguments  file pointer and the string of data which is used to be written.Optionally  third integer is used foe length of the data to write.If the third argument is used , file writing would stop when it reached to its specified length.

File Closing

The fclose() function is used for closing a file it  requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails. Php closes all your files after using them because the file will auto close after PHP code finishes its execution.

 

 

 

Keywords: