Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the Ds\Vector rotate() function in PHP?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Vector class in PHP is an array that is automatically resized. The elements of a Vector instance are accessible via indexes.

This shot discusses the rotate method of the Vector class.

Description

The rotate method rotates the current Vector instance based on the number of rotations specified by the $rotations parameter.

Syntax

<?php
public Ds\Vector::rotate(int $rotations): void
?>

Parameters

  • $rotations is an integer that specifies how many elements are to be removed from the start of the Vector and pushed at the end.

Return value

None.

Exceptions

None.

Example

<?php
$vec = new \Ds\Vector([1, 2, 3, 4, 5]);
$vec->rotate(1);
print_r($vec);
$vec->rotate(2);
print_r($vec);
?>

Output:

Ds\Vector Object
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 1
)
Ds\Vector Object
(
[0] => 4
[1] => 5
[2] => 1
[3] => 2
[4] => 3
)

In the example above, a vector $vec is initialized with the values 1, 2, 3, 4, and 5. The rotate method is invoked twice on $vec. The first rotate call is passed 1 as the $rotation argument, which results in 1 getting removed from the start of $vec and placed at the end of $vec. The second rotate call is passed 2 as the $rotation argument, which results in 2 and 3 getting removed from the start and subsequently placed at the end of $vec.

RELATED TAGS

php

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring