Classes, Properties, Constructors, Objects, Namespaces

Slides:



Advertisements
Similar presentations
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Advertisements

Sets, Dictionaries SoftUni Team Technical Trainers Software University
Inheritance Class Hierarchies SoftUni Team Technical Trainers Software University
Stacks and Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Generics SoftUni Team Technical Trainers Software University
Exporting and Importing Data
Auto Mapping Objects SoftUni Team Database Applications
Static Members and Namespaces
Functional Programming
Databases basics Course Introduction SoftUni Team Databases basics
Abstract Classes, Abstract Methods, Override Methods
Sets, Hash table, Dictionaries
C# Basic Syntax, Visual Studio, Console Input / Output
Interface Segregation / Dependency Inversion
Data Structures Course Overview SoftUni Team Data Structures
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
Reflection SoftUni Team Technical Trainers Java OOP Advanced
Application Architecture, Redux
Mocking tools for easier unit testing
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
OOP Course - Virtual Trip
Processing Sequences of Elements
EF Relations Object Composition
Entity Framework: Code First
Repeating Code Multiple Times
Inheritance Class Hierarchies SoftUni Team Technical Trainers C# OOP
Java OOP Overview Classes and Objects, Members and Class Definition, Access Modifier, Encapsulation Java OOP Overview SoftUni Team Technical Trainers.
Basic Tree Data Structures
Databases advanced Course Introduction SoftUni Team Databases advanced
Install and configure theme
Balancing Binary Search Trees, Rotations
Entity Framework: Relations
Fast String Manipulation
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
Combining Data Structures
Best practices and architecture
Arrays and Multidimensional Arrays
Built-in Functions. Usage of Wildcards
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
Exporting and Importing Data
Making big SPA applications
Manual Mapping and AutoMapper Library
Functional Programming
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Exporting and Importing Data
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Iterators and Comparators
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Reflection SoftUni Team Technical Trainers C# OOP Advanced
Spring Data Advanced Querying
Software Quality Assurance
JavaScript Frameworks & AngularJS
Polymorphism, Interfaces, Abstract Classes
/^Hel{2}o\s*World\n$/
CSS Transitions and Animations
Multidimensional Arrays
Presentation transcript:

Classes, Properties, Constructors, Objects, Namespaces OOP - Basics Classes, Properties, Constructors, Objects, Namespaces SoftUni Team Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Table of Contents Classes and Objects in OOP OOP in PHP Define Simple Classes Creating Classes and Objects Using Namespaces © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Questions sli.do #PHPFUND

Defining Simple Classes

Class Versus Instance Classes model real-world objects

Classes and Objects PHP supports Object-Oriented Programming (OOP) Supports custom classes, objects, interfaces , namespaces, traits Like other OOP languages (C#, Java, C++) class Rock { public $height = 12; function fall() { $this->height--; } $myRock = new Rock(); $myRock->fall(); echo $myRock->height; // 11

Defining and Using Data Properties

Property declarations Properties Properties hold the internal object state They have visibility which should be defined at declaration class Dog { public $name; public $breed; public $age; public $children; } Property declarations

Properties and Classes Live Demo

Defining and Using Class Constructor * Constructor Defining and Using Class Constructor (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

As a rule the constructor should initialize all class properties * Defining Constructor "$this" points to the current instance of the class class Person { public $name; public $age; function __construct() { $this->name = null; $this->age = 0; } As a rule the constructor should initialize all class properties (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Defining Constructor (2) * Defining Constructor (2) The constructor may optionally have parameters class Person { public $name; public $age; function __construct(string $name, $age) { $this->name = $name; $this->age = $age; } Constructor with parameters (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Constructor Live Demo * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Defining and Using Class Methods * Methods Defining and Using Class Methods (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

* Defining Methods Methods are classes own functions and define behavior of the class Simple method with no arguments class Person { public $name; public $age; function printNames(): void { echo $this->name . $this->age; } "void" return type is available since PHP 7.1 (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Method with parameters * Defining Methods (2) class Person { public $name; public $age; function printNames(string $name): string { $this->name = $name; return $this->name; } Method with parameters (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Problem: Define a Bird Class Create properties age weight flyingSpeed Create methods to model bird's behavior breathe() walk() fly()

Solution: Define a Bird Class class Bird { private $age; private $weight; private $flyingSpeed; public function __construct($age, $weight, $flyingSpeed) { $this->age = $age; $this->weight = $weight; $this->flyingSpeed = $flyingSpeed; }

Solution: Define a Bird Class public function walk() { echo "Walking" . "\n"; } public function breath() { echo "Breathing" . "\n"; public function fly() { echo "Flying" . "\n";

More on Classes and Objects * Anonymous Objects More on Classes and Objects (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Classes and Objects – Example class Student { public $name; public $age; public function __construct($name = null, $age = null) { $this->name = $name; $this->age = $age; } $peter = new Student("Peter", 21); echo $peter->name; $peter->age = 25; print_r($peter); // Student Object ( [name] => Peter [age] => 25 ) $maria = new Student('Maria'); print_r($maria); // Student Object ( [name] => Peter [age] => )

Anonymous Objects The stdClass is an empty generic PHP class for initializing objects of anonymous type (e.g. $obj = new stdClass;) It is NOT the base class for objects in PHP Objects can contain their own properties e.g. $obj->prop = value; $anonCat = new stdClass; $anonCat->weight = 14; echo 'My cat weighs ' . $anonCat->weight . ' kg.'; // My cat weighs 14 kg.

Anonymous Objects – Example $person = new stdClass; $person->name = 'Chinese'; $person->age = 43; $person->weapons = ['AK-47', 'M-16', '9mm-Glock', 'Knife']; echo json_encode($person); // {"name":"Chinese","age":43,"weapons":["AK-47","M-16","9mm-Glock","Knife"]} $obj = (object)['name' => 'Peter', 'age' => 25]; $obj->twitter = '@peter'; echo json_encode($obj); // {"name":"Peter","age":25,"twitter":"@peter"}

Live Exercises in class * Classes and Objects Live Exercises in class (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Namespaces Namespaces are used to group code (classes, interfaces, functions, etc.) around a particular functionality Better structure for your code, especially in big projects Classes, functions, etc. in a namespace are automatically prefixed with the name of the namespace (e.g. MVC\Models\Lib1\Class1) Using a namespace in PHP: use CalculationsManager; $interest = CalculationsManager\getCurrentInterest(); $mysqli = new \mysqli("localhost", "root", "", "world");

Namespaces – Example Declares the use of given namespace <?php namespace SoftUni { function getTopStudent() { return "Pesho"; } namespace NASA { use SoftUni; $topSoftUniStudent = SoftUni\getTopStudent(); echo $topSoftUniStudent; // Pesho ?> Declares the use of given namespace Uses a function from the SoftUni namespace

Namespaces – Example (2) Define the class in separate file namespace SoftUni; class Student { public $fname = "Gosho"; public $lname = "Pesho"; public function printNames() { echo $this->fname . $this->lname . "\n"; }

Namespaces – Example (2) Include the previous file namespace SoftUni2; require_once 'softuni.php'; use SoftUni\Student; function createStudent() { $student = new Student('Gosho', 'Petrov'); return $student; } print_r(createStudent());

Namespaces Exercises in class * (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Summary Classes define specific structure for objects Objects are particular instances of a class Classes define properties, constructor and other members Constructor is invoked when creating new class instances and initialize the object's internal state PHP supports classes, objects and anonymous objects PHP supports namespaces to split program logic into modules © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

OOP Basics https://softuni.bg/courses/php-basics/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "PHP Manual" by The PHP Group under CC-BY license "PHP and MySQL Web Development" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.