Php coding using class

How To Use Classes In PHP

Classes are reusable, self-contained modules of code, used for specific purposes.

Classes are a great way to organize code because, as mentioned, they’re both reusable and self-contained.

Below, I’m going to give you a rundown of how to create and use classes in PHP.

Creating a class

To define a class, you use the following syntax:

Classes can contain variables and functions

So, within a class, you can define variables and functions:

 class Html < public $p_open = '

'; public $p_close = '

'; public function printParagraph($text) < >>

And, when referencing a class’s own variables from within one of its functions, you use the $this to refer to the class itself:

 class Html < public $p_open = '

'; public $p_close = '

'; public function printParagraph($text) < // prints

value-of-text

echo $this->p_open . $text . $this->p_close; > >

But how do you access that functionality? Ahh, yes … by creating an instance of the class.

Creating an instance of a class

If you think of the class as sort of a “template”, you can create an “object” based on that template; that “object” is called a class instance.

You create an instance of a class using the new keyword, followed by the class’s name:

// creates an instance of the Html class $myPage = new Html; 

And then you can access all of the class’s functionality — via your instance — using the familiar arrow, -> , syntax:

// creates an instance of the Html class $myPage = new Html; // prints 

Whoomp, there it is!

$myPage->printParagraph('Whoomp, there it is!');

Classes also have the ability to borrow the functionality of other classes.

Extending a class

A class can extend another class in order to “borrow” the other class’s functionality.

So, let’s say we have a Vehicle class with variables to represent the vehicle’s info, and a function to display its info:

 class Vehicle < public $make = ''; public $model = ''; public $year = ''; public function printInfo() < // prints " " echo $this->year . ' ' . $this->make . ' ' . $this->model; > > 

But then let’s say we then want to create a Car class to represent a specific type of vehicles, cars. We can create that class and have it extend the Vehicle class — using the extends keyword — which will let it:

  1. borrow all of the Vehicle class functionality;
  2. and add its own Car -specific functionality.
 class Car extends Vehicle < public function honk() < // prints "honk, honk!" echo "honk, honk!"; >> // a car! $car = new Car; // this all works, because Cars // have access to Vehicle functionality $car->year = 1981; $car->make = 'DeLorean'; $car->model = 'DMC-12'; // prints "1981 DeLorean DMC-12" $car->printInfo(); // but it can *also* honk — specific to Cars $car->honk(); // prints "honk, honk!" 

Last, but not least … what’s all this business with public ?! Is there a private ?

public vs. private

Let me make this as simple as possible.

When you precede a variable or function in a class with public , the variable/function can be accessed outside of the class itself.

When you precede a variable or function in a class with private , the variable/function can only be accessed from within the class.

So, let’s take real-life example. Say you wanted to create a Database class to establish database connections. That class would need the connection credentials, but it only needs to access them from within the class itself.

On the other hand, it needs to access the database connection outside of the class — otherwise it’d be quite useless!

You could define such a class like so:

 class Database < // only accessible *inside* of the class private $servername = 'changeme'; private $username = 'change'; private $password = 'change'; // accessible *outside* of the class public $connection = null; public function connect() < // INSIDE the class, so can access it all! $this->connection = connect_function($this->servername, $this->username, $this->password); > > // Connect to the database! // works because the connect function is public $db = new Database; $db->connect(); // use the connection for something (e.g. to query) // works because the connection variable because it's public $result = $db->connection->query('…'); 

Want to learn to build and deploy a Laravel web application, step by step?

From Idea To Launch – An Online Course + Community

In From Idea To Launch, my online video course + community, I’ll walk you through the entire process of building and launching your own, complete Laravel web application, at beginner’s speed. I’ll teach you all the PHP and Laravel fundamentals you need to know as you build your application, step by step. And you’ll have access to all the help and assistance you need as you move through the course.

© 2023 SelfTaughtCoders.com. Made with ❤ in Los Angeles, California.

Источник

PHP OOP — Classes and Objects

A class is a template for objects, and an object is an instance of class.

OOP Case

Let’s assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Define a Class

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces (<>). All its properties and methods go inside the braces:

Syntax

Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>
?>

Note: In a class, variables are called properties and functions are called methods!

Define Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class are created using the new keyword.

In the example below, $apple and $banana are instances of the class Fruit:

Example

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
>

$apple = new Fruit();
$banana = new Fruit();
$apple->set_name(‘Apple’);
$banana->set_name(‘Banana’);

echo $apple->get_name();
echo «
«;
echo $banana->get_name();
?>

In the example below, we add two more methods to class Fruit, for setting and getting the $color property:

Example

class Fruit // Properties
public $name;
public $color;

// Methods
function set_name($name) $this->name = $name;
>
function get_name() return $this->name;
>
function set_color($color) $this->color = $color;
>
function get_color() return $this->color;
>
>

$apple = new Fruit();
$apple->set_name(‘Apple’);
$apple->set_color(‘Red’);
echo «Name: » . $apple->get_name();
echo «
«;
echo «Color: » . $apple->get_color();
?>

PHP — The $this Keyword

The $this keyword refers to the current object, and is only available inside methods.

Look at the following example:

Example

So, where can we change the value of the $name property? There are two ways:

1. Inside the class (by adding a set_name() method and use $this):

Example

class Fruit public $name;
function set_name($name) $this->name = $name;
>
>
$apple = new Fruit();
$apple->set_name(«Apple»);

2. Outside the class (by directly changing the property value):

Example

class Fruit public $name;
>
$apple = new Fruit();
$apple->name = «Apple»;

PHP — instanceof

You can use the instanceof keyword to check if an object belongs to a specific class:

Источник

Читайте также:  Стилизация select css стрелочки
Оцените статью