PHP-Advanced

 <Beginner PHP><Array><Loop><Function><Pre-Def variable><Session><Read Write File><CRUD>

 

Object Oriented PHP

Classes & Objects in PHP

OOP is intended to make thinking programming closer to the real world.
Focal point of OOP are classes.
Objects are created using classes

The Class describes what the object will be
The class is separate from the object itself.

Class Building Dog Computer
Object
(or instance)
Empire
State
GM Ford VW Lassie K9 PitB Apple IBM HP
Features of objects x/y/z x y z
Function
Methods
Behaviour of
objects
a/b/c a b c

A class name starts with letter or _

class Person {
public $age; // property
public function speak () { //method
echo "hil";
}

Public: it is a visibility specifier. It can be accessed from anywhere in the code.

PHP Objects

instantiation = it is the process of creating an object of a class
new= the keyword for instantiate.
$bob=new Person();

  • object : $bob
  • class : Person()
-> this arrow is for letting access to the properties and methods

echo $bob->age;

  • property: age property for $bob
= it assigns a value (to a property)
class Person{               // class
public $age;                 //property
    function speak(){    //method
    echo "Hi!";
    }
}
$p1=new Person();     // object: $p1  of the class: Person() is instantiated (new)  
$p1->age=23              // property: age  of object: $p1 is accessed here // it is also assigned
echo $p1->age;           // output: 23 
$p1->speak();             // output: Hi!

Note:
  • when we get access to a function, it is automatically executed (TR: ne demek bu?)
  • A property is not something to execute. It needs few steps (TR: anlamadim, neden bu not?)
    • assign a value : age=23 
    • Echo : echo $p1->age;
$this = it is a pseudo-variable. It is a ref to the calling object. We use it within a method. We use it in same way as object.

Notes for myself (TR: Kendime notlar)
  • $this is helping the function to have 
    • different property of object, 
    • variable values, 
    • changed easily : it helps change the value assign to property
  • it avoids us using echo repeatedly
  • it simplifies execution of the function
class Dog {
    public $legs=4;
        public function display(){
            echo $this->legs;
        }
}
$d1=new Dog();
$d1->display(); //4

$d2=new Dog();
$d2->$legs=2 //
$d2->display(); //2 (TR:anlamadim, PHP objects`in tutorial`ini tekrar calis)

PHP Class Constructor

_construct()  = it is called automatically whenever a new object is instantiated

class Person {
public $name;
public $age;
public function _construct($name, $age){
$this->name = $name;
$this->age= $age;
echo "Hi I am ";
echo $this->name;
echo ". I am";
echo $this->age;
echo" years old. <br />";
}
}
$p=new Person("David",42);
$s=new Person("Pedro",49);
/* output
Hi, I am David. I am 42 years old
Hi, I am Pedro. I am 49 years old 
*/

PHP Class Destructor

_destruct() = it is called automatically when an object is destroyed

class Person{
public function _destruct(){
echo "object nanay";
}
}
$p=new Person();
when the script ends, the object is automatically destroyed

other function similar
unset()

Example: unset($p)

Destructors are good for
  • release ressource
  • write log files
  • close a database connection
  • etc...

PHP Class inheritance

Classes can inherit the methods and properties of another class

sub class : class thet inherits
parent class : the 'from' class

extends : keyword used for inheriting

class animal{
public $name;
public function hi(){
echo "hey wof";
}
}
class dog extends animal{
}
$d=new dog();
$d->hi();
// output : hey wof

Note: 
  • constructor in sub class makes inheritance difficult
  • Parent should be public for inheritance (visibility public)

PHP Visibility

Visibility is for properties and methods
It defines/controls how and from where they can be accessible

Two keywords to declare visibility:
protected: members are accessible only within the class itself by inherit and by class itself
private: members are accessible only by the class that defines them

Class properties must always have a visibility type

Methods with no visual keyword are defined as public
with inheritance
Private members are used internally in a class

PHP Interfaces


It specifies a list of method that a class must implement
  • it does not contain any method implementation
  • it allows a method to be handled differently in each class that use interfaces
Keyword: 
interface : defining an interface
implements: it is used to implement an interface in each class


interface AnimalInterface{
public function makesound();
}
class Dog implements AnimalInterface{
public function makesound(){
echo "woof";
class cat implements AnimalInterface{
public function makesound(){
echo "meow";
}
$myobj1= new Dog();
$myobj1->makesound();
$myobj2=new Cat();
$myobj2->makesound();
?>

Note
TR: galiba method`larda kolaylık sağlıyor

A class can implement multiple interfaces
To specify multiple interface separate them with commas

class Demo implements AInterface, BInterface, CInterface

//functions
}

An interface can be (?) inherit another interface
bey keyword: extends

All the methods specified in an interface require public visibility

Interface:
it is important in OOP
it is like contract or rules of game
it enforce

PHP abstract classes
Abstract classes can be inherited but not instantiated

advantage:
can contain both methods
  • with definitions  
  • and abstract methods
both are not defined until inherited

A class inheriting from an abstract class must implement all the abstract methods

Keyword: 
abstract: creates abstract class or abstract method

abstract class Fruit {
private $color;
abstract public function eat();
public function setcolor($c){
$this->color=$c;
}
}
class Apple extends Fruit {
public function eat(){
echo "Omnomnom";
}
}
$obj = new Apple();
$obj->eat();

Abstract functions can only appear in an abstract class

Meth nameProp name
InterfaceX
Abstract ClassXX
write to db file shorten amount of code 

Static

keyword: static

It defines static properties and static methods

A static property or method can be accessed without creating and object of that class

A static property or method is accessed by using :: (=scope resolution operator) between the class name and property/method name

class myClass{
static $myStaticProperty=42;
}
echo myClass:: $myStaticProperty;

keyword: self

It is needed to access a static property from a static method in a class definition

class myClass{
static $myProp=42;
static function myMeth(){
echo self:: $myProp;
}
}
myClass::myMeth();


Object of a class can not access static property in a class but they can access static method
If rate is 
`a` in year x1 than on t1 acc*a
`b` in year x2 then on t2 acc*b
(TR: yani önceki veriler değişmemeli)

Final

keyword: final
it defines method that can not be overridden in child classes

Classes that are defined final can not be inherited

class myClass1{
final function myFunction(){
echo"Parent";
}
}
//Error because final is in my Class
class myClass2 extends myClass1{
function myFunction(){
echo "Child";
}
}
Properties can not be marked final

Include


Other advanced tools:

PHPStan

it is a static analysis tool that allows you to find bugs in your codebase without running the code. The phpstan-drupal extension for PHPStan makes it possible to perform static analysis of Drupal code.

souce: 

Rector

it is a PHP tool that you can run on any PHP project to get an instant upgrade or automated refactoring. It helps yous especially with: PHP upgrades, framework upgrades, improving your code quality.


PHPCS 

it is an open-source CLI tool that detects code style violations of a defined coding standard, and also provides automatic fixes for automatically fixable rules.


PHP CS Fixer

The PHP Coding Standards Fixer (PHP CS Fixer) is a tool designed to automatically fix PHP coding standards issues.


phpcpd


PHP Copy/Paste Detector is a Copy/Paste Detector for PHP code.

It scans a PHP project for duplicated code, in order to "Don't Repeat Yourself" (DRY).

source: https://phpqa.io/projects/phpcpd.html

Comments

Popular posts from this blog

Programming- AI, machine learning

Programming -- JS, C, C++, Python

links - Robotic