Php – Class

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-class/
PINTEREST
PINTEREST
INSTAGRAM

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations

Php programming language supports most of the Object Oriented Programming concepts. Object Oriented Programming provides a lot of useful features to manage big and enterprise applications.

Object Oriented Programming

Object Oriented Programming or OOP is a concept that provides different features to implement real world problems. Up to now we have used variables to write Php applications but as you know enterprise applications can not work with those simple types.

For example we may need to define a student in our app. What kind of properties or actions may have a student in real life. Here an example list;

  • Name
  • Surname
  • Age
  • Class
  • Parents
  • Select Class
  • Finish School
  • Add Missed Days

We can resume the list but these are enough. Using only primitive data types like string, integer etc. we can not cope all of these things. Assume that we have 1000 students in school. Should we create separate variables for each students name? No. Solution is Object Oriented Programming and Class

Define Class

Class is used to specify composite types. We can use different type of variables and methods in a class. Class makes them easy to access and use. Classes can be used for other class definitions which makes complex class inheritance an easy job. We can create a class with class keyword and curly brackets {} which is class body. Here is the syntax

LEARN MORE  Access.conf Security Configuration For Linux and Unix

In this example we will create a class named Student which have some properties and methods.

Just concentrate the class definition and how its body look. We will learn properties and methods in next steps.

Create Object

We have defined a class in previous step but how can we use this class. Classes can not be used directly. Because classes only defines the structure and in most situations do not hold data. We should initialize the class by creating object from it with new keyword. Objects holds given data according to their class definitions.

In this example we will use Student class to create an object named john . john will represent the methods and attributes of Student class.

Class Properties

Properties are used to store  some data . Another usage of properties are access to data from object directly. Properties types are primitive types like integer, string, float, boolean etc.

LEARN MORE  C Functions - Create and Run with Examples

In this example we will add Student class surname, number and age properties.

Access Class Properties Inside Class

We generally need to access class properties inside class. We can use $this keyword in order to access. We will also use ->   as delimiter between $this and property name. In the function named SayName we used $this-> to access  name property of the class.

Access Class Properties

We can access class properties with the object and property name. We will use -> to address  property name.

In this example we will access the $name and $surname properties of the $john object.

Class Methods

One of the powerful feature of the class and object is methods. We can define methods in class and call them over objects. We can easily access object properties in these methods too.

LEARN MORE  C - Structures

In this example we will create a function named PrintStudent() which will print name and surname student informations;

PHP Tutorial Topics

  1. Introduction To PHP
  2. Installing PHP To Linux
  3. Installing PHP To Windows
  4. PHP Hello World Project with Eclipse
  5. PHP Syntax
  6. PHP Variables and Types
  7. PHP String Types
  8. PHP Numeric Type (Integer, Float, Double)
  9. PHP Boolean Type
  10. PHP Arrays
  11. PHP Operators
  12. PHP Ternary Operators
  13. PHP Constants
  14. PHP If..Elseif..Else Conditional Statements
  15. PHP Switch..Case Conditional Statements
  16. PHP For and Foreach Loop Statements
  17. PHP While and Do While Statements
  18. PHP Functions
  19. PHP Class and Objects
  20. PHP Date and Time Operations
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/php-class/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)