Php – Class – POFTUT

Php – Class


[rps-include post=6522]

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

class NAME {

BODY

}

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

class Student{

    public $name="";

    public function SayName(){

    echo $this->name;

    }

}

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.

LEARN MORE  C Functions - Create and Run with Examples

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 Student{

    public $name="";

    public function SayName(){

    echo $this->name;

    }

}


$john = new Student;

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.

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

class Student{

    public $name="";
    public $surname="";
    public $number="";
    public $age=-1;

    public function SayName(){

    echo $this->name;

    }

}

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.

class Student{

    public $name="";
    public $surname="";
    public $number="";
    public $age=-1;

    public function SayName(){

    echo $this->name;

    }

}

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 Student{

    public $name="";
    public $surname="";
    public $number="";
    public $age=-1;

    public function SayName(){

    echo $this->name;

    }

}

$john = new Student();

echo $john->name." "$john->surname;

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.

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

class Student{

    public $name="";
    public $surname="";
    public $number="";
    public $age=-1;

    public function SayName(){

    echo $this->name;

    }

    public function PrintStudent(){

        echo $this->name;

        echo $this->surname;

    }

}

$john = new Student();

$john->PrintStudent();

[rps-include post=6522]

Leave a Comment