Amr Fekry

Traits

Amr Fekry1 week ago

As we have seen, interfaces help you manage the fact that, like Java, PHP does not support multiple inheritance. In other words, a class in PHP can only extend a single parent. However, you can make a class promise to implement as many interfaces as you like; for each interface it implements, the class takes on the corresponding type.So interfaces provide types without implementation. But what if you want to share an implementation across inheritance hierarchies? PHP 5.4 introduced traits, and these let you do just that.

A trait is a class-like structure that cannot itself be instantiated but can be incorporated into classes. Any methods defined in a trait become available as part of any class that uses it. A trait changes the structure of a class, but doesn’t change its type. Think of traits as includes for classes.Let’s look at why a trait might be useful.

A Problem for Traits to Solve

Here is a version of the ShopProduct class with a calculateTax() method:

<?php
Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Interfaces

Amr Fekry1 week ago

Although abstract classes let you provide some measure of implementation, interfaces are pure templates. An interface can only define functionality; it can never implement it. An interface is declared with the interface keyword. It can contain properties and method declarations but not method bodies.

<?php
interface Chargeable
{    
    public function getPrice(): float;
}
Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Abstract Classes

Amr Fekry1 week ago

An abstract class cannot be instantiated. Instead it defines (and, optionally, partially implements) the interface for any class that might extend it.You define an abstract class with the abstract keyword. Here I redefine the ShopProductWriter class I created in the previous post, this time as an abstract class:

<?php
abstract class ShopProductWriter
{    
    protected $products = [];    
    
    public function addProduct(ShopProduct $shopProduct)    {
Continue reading
PHP#PHPOOP#OOP
Amr Fekry

Constant Properties

Amr Fekry1 week ago

Some properties should not be changed. The Answer to Life, the Universe, and Everything is 42, and you want it to stay that way. Error and status flags will often be hard-coded into your classes. Although they should be publicly and statically available, client code should not be able to change them. PHP allows you to define constant properties within a class. Like global constants, class constants cannot be changed once they are set. A constant property is declared with the const keyword. Constants are not prefixed with a dollar sign like regular properties. By convention, they are often named using only uppercase characters:

<?php
class ShopProduct
{    
    const AVAILABLE      = 0;
    const OUT_OF_STOCK   = 1;
}
Continue reading
OOP#OOPPHP#PHP
Amr Fekry

All of the examples in the previous posts worked with objects. I characterized classes as templates from which objects are produced, and objects as active instances of classes the things whose methods you invoke and whose properties you access. I implied that, in object-oriented programming, the real work is done by instances of classes. Classes, after all, are merely templates for objects.In fact, it is not that simple. You can access both methods and properties in the context of a class rather than that of an object. Such methods and properties are static and must be declared as such by using the static keyword:

<?php

class StaticExample
{    
Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Arguments and Types

Amr Fekry1 week ago

Type determines the way data can be managed in your scripts. You use the string type to display character data, for example, and manipulate such data with string functions. Integers are used in mathematical expressions, booleans are used in test expressions, and so on. these categories are known as primitive types. On a higher level, though, a class defines a type. A ShopProduct object, therefore, belongs to the primitive type object, but it also belongs to the ShopProduct class type. In this section, I will look at types of both kinds in relation to class methods.Method and function definitions do not necessarily require that an argument should be of a particular type. This is both a curse and a blessing. The fact that an argument can be of any type offers you flexibility. You can build methods that respond intelligently to different data types, tailoring functionality to changing circumstances. This flexibility can also cause ambiguity to creep into code when a method body expects an argument to hold one type but gets another.

PHP is a loosely typed language. This means that there is no necessity for a variable to be declared to hold a particular data type. The variable $number could hold the value 2 and the string "two" within the same scope. In strongly typed languages, such as C# or Java, you must declare the type of a variable before assigning a value to it, and, of course, the value must be of the specified type.This does not mean that PHP has no concept of type. Every value that can be assigned to a variable has a type. You can determine the type of a variable’s value using one of PHP’s type-checking functions.

Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Constructors

Amr Fekry1 week ago

A constructor method is invoked when an object is created. You can use it to set things up, ensuring that essential properties are assigned values, essential methods are invoked and any necessary preliminary work is completed.

<?php

class ShopProduct
{    
    public $title;
    public $producerMainName;
Continue reading
PHP#PHPOOP#OOP
Amr Fekry

Methods

Amr Fekry1 week ago

Just as properties allow your objects to store data, methods allow your objects to perform tasks. Methods are special functions declared within a class. As you might expect, a method declaration resembles a function declaration. The function keyword precedes a method name, followed by an optional list of argument variables in parentheses. The method body is enclosed by braces:

<?php
public function myMethod($argument, $another)
{        
    // ...    
}
Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Objects

Amr Fekry2 weeks ago

If a class is a template for generating objects, it follows that an object is data that has been structured according to the template defined in a class. An object is said to be an instance of its class.

The new operator is invoked with a class name as its only operand and returns an instance of that class.

$product1 = new ShopProduct();
$product2 = new ShopProduct();

If you are still confused, Think of a class as a cast in a machine that makes plastic ducks. Our objects are the ducks that this machine generates. The type of thing generated is determined by the mold from which it is pressed. The ducks look identical in every way, but they are distinct entities. In other words, they are different instances of the same type. The ducks may even have their own serial numbers to prove their identities. Every object that is created in a PHP script is also given its own unique identifier. (Note that the identifier is unique for the life of the object; that is, PHP reuses identifiers, even within a process). I can demonstrate this by printing out the $product1 and $product2 objects:

Continue reading
OOP#OOPPHP#PHP
Amr Fekry

Classes

Amr Fekry2 weeks ago

In short, a class is a code template used to generate one or more objects And a class is an entity that determines how an objects will behave and what the objects will contain, in other words it's a blueprint to build to build a specific type of objects

You declare a class with the class keyword and an followed by the class name. Class names can be any combination of numbers and letters, although they must not begin with a number. The code associated with a class must be enclosed within braces.

<?php
class ShopProduct
{    
    // class body
Continue reading
PHP#PHPOOP#OOP
Amr Fekry

Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of oop is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

OOP#OOPPHP#PHP
Mohamed Saad

One of the most important and helpful concepts is open source. It keeps our field, specially software, dynamic and active. Every developer should contribute in an open source project. Technologies and tools like React & Lodash are open source and waiting for contribution to enhance it and keep it efficient and great. Let's see how is it going...

What we'll talk about

  • What's open source?

  • Why is it so important to know?

  • How to contribute to open source projects?

Continue reading
Programming Concepts#Programming Concepts
Muhammed Yousrii

Creating links - Accessibility side

Muhammed Yousrii5 months ago

Introduction

Accessibility is one of the most ignored subject between developers in day-to-day developing. And I argue you to visit Accessibility brief to know how accessibility is an important factor to concern on your next project


Anchor tag

The anchor tag is approximately the main building block of any project you worked on or you will work on, It can be used to refer to another:

Continue reading
HTML5#HTML5HTML#HTML
Hasan Zohdy

Pure Functions is part of Functional Programming that a function doesn't depend on any external resources such as global variables, other functions, any type of remote requests or anything else except The given inputs which are the function arguments.

Another key of Pure Functions that it MUST NOT have a side effect such as throwing an error, logging or calling another function that may produce such things like these.

So Pure Functions are simply function that perform certain operations on the given arguments only that will always, always return same result.

Let's see some examples to clarify it.

// js snippet code
Continue reading
Programming Concepts#Programming Concepts
Hasan Zohdy

What is Monkey Patch

Hasan Zohdy7 months ago

What is Monkey Patch?

In simple terms, Monkey Patch is a way to extend or modify an existing program or code.

Modifying existing code

Let's say we want to change Math.PI from 3.141592653589793 to be 3.2

We can do it in that way:

Continue reading
Programming Concepts#Programming ConceptsJAVASCRIPT#JAVASCRIPT
shreen taher

What's New in PHP 7.4

shreen taher7 months ago

In this post we’re covering several changes and features that should be added to the language with the final release of PHP 7.4, which is scheduled to be released in November of 2019 :

Arrow functions

Arrow functions, also called "short closures", are a way of writing shorter functions in PHP.

// A collection of Post objects
$posts = [/* … */];

$ids = array_map(fn($post) => $post->id, $posts);

Continue reading
PHP#PHP
shreen taher

What's New in Laravel 5.6

shreen taher7 months ago

The major highlights of Laravel 5.6 :-

API Controller Generation

Laravel 5.6 now offers API controllers that reduces the number of unnecessary create and edit actions that only return HTML. Use the following command to use API controller generation:

php artisan make:controller API/PhotoController --api

Eloquent Date Casting

Laravel 5.6 allows developers to easily change date format as per the project’s requirements.

Continue reading
PHP#PHPLARAVEL#LARAVEL
Hasan Zohdy

As web development grew up rapidly in the last few years, creating a website now takes different approach than the past.

Nowadays there are two ways to develop a website:

  • Single Page Application SPA.

  • Multi Page Application MPA.

What is Singe Page Application

Continue reading
smackdown#smackdown
shreen taher

What's New in Laravel 5.5

shreen taher7 months ago

Laravel 5.5 will require PHP 7.0+.

Sreamlined Request Validation

  • you can now directly call the validate method on your Request instance.
  • You also no longer need to pass the request as the first argument to the validator.
  • The second change made is that the validator returns the request data which you can store in a variable and pass on to the create method of the model.
public function store()
{
    $post = request()->validate([
Continue reading
LARAVEL#LARAVELPHP#PHP
Mohamed Samir

PHP Standards Recommendations [PSR-2]

Mohamed Samir8 months ago

# General

  • Code MUST follow a "coding style guide" PSR [PSR-1]
<?php
namespace Vendor\Model;

class Foo
{
    const VERSION = '1.0';
    const DATE_APPROVED = '2019-06-01';
Continue reading
PHP#PHPOOP#OOP
Hasan Zohdy

Ceil VS Floor VS Round

Hasan Zohdy8 months ago

Almost in every programming language there is a Math class that offers these three methods ceil, floor and round, so what's the difference between them.

Rounding numbers

First off, let's understand what does these functions do.

In simple words, these methods round floats numbers to be integers but the point here is to what number they round these fractions?

Ceil

Always the ceil method rounds any float to the next integer.

Continue reading
smackdown#smackdown
shreen taher

What's new in php 7.3 ?

shreen taher8 months ago

is_countable check if something is countable.

When counting uncountable objects,you get warning. The is_countable function can help prevent this warning.

Note This is New feature in PHP 7.3 not exist in php 7.2 or lower version.

bool is_countable ( mixed $var )
$count = is_countable($variable) ? count($variable) : null;
Continue reading
PHP#PHP
Hasan Zohdy

PHP PSR-1: Basic Coding Standard

Hasan Zohdy8 months ago

What is PSR

And why it's that important to know?

Who uses PSR?

PSR stands for PHP Standards Recommendations.

PHP Framework Interop Group! We're a group of established PHP projects whose goal is to talk about commonalities between our projects and find ways we can work better together.

Continue reading
PHP#PHP
Hasan Zohdy

These three are types of naming conversions used to declare variables, constants, functions and classes names.

Studly Case

StudlyCaseWords Also called Pascal Case which makes the first letter of each word capital with no space or underscore between words.

Used in

Usually this type of naming conversion used with class names in OOP in general

Continue reading
smackdown#smackdown