Skip to content

Introduction to OOP with PHP

php oop image 1

PHP including, the other program languages almost all have OOP structure. That has certain reasons. For example; You are software engineer in a company. Suppose you write codes in this company without OOP. (Another name of this is spaghetti coding). If after a while you will leave work, the new software engineer will have difficulty understanding your code.

Codes written using OOP are more understandable. And these codes understandings easy. Probably, no company can’t grow without OOP codes. Therefore you have to learn OOP.

Object Oriented Programming Language is an approach adapted to programming in real life relational models. Simply put, classes encoded in OOP languages ​​correspond to objects in real life. Classes have property and methods. Basic principles of OOP languages:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance


  • Class names should always be used meaningfully.
  • Alphanumeric values ​​(a-z A-Z 0-9) and _ (underscore) can be used in class names.
  • Class names must begin with a letter or _ (underscore).
  • Class names can never start with a number.
  • Names / definitions used by PHP cannot be used when defining class names.
  • Class names can never be used more than once. (Namespace is used more than once.)
  • Class names are not case-sensitive.
  • Class is subject to coverage / domain rules.

Properties Used in Object Oriented Programming


A name given to objects that have already been prepared to perform one or more tasks in any programming.

	class MyClass{


The new keyword is used to create a new object from the class. for example new MyClass(); as.

	class MyClass{

	$myClass = new MyClass;


Allows you to access the variable or method of a class from within the class. The variable $this refers to our class.

	class MyClass{
		public $name = 'truecodes';
		public function myFunction(){
			return $this->name;
	$myClass = new MyClass;

Public, Private and Protected

The public keyword allows the variable or metho to be accessed outside the class.

The keyword private prevents our variable or method from being accessed from outside the class. The main reason for this prevention is to prevent a security vulnerability.

The protected keyword makes it possible to use class variables or methods in classes derived from that class. As with the private keyword, access from outside the class is not allowed. The protected keyword is used when we inherit.

	class MyClass{
		public $var1    = 'public';
		private $var2 	= 'private';
		protected $var3 = 'protected';


	class Employee{
		private $name    = 'furkan';
		private $surname = 'gulsen';
		public $age      = 21;
    public $from     = 'TURKEY';
    public function _print(){
      return "Name: $this->name<br/>Surname: $this->surname";

  $employee = new Employee();
  echo $employee->_print();
  echo '<br/>age : ' . $employee->age;
  echo '<br/>from: ' . $employee->from;

Name: furkan
Surname: gulsen
age : 21
from: TURKEY

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: