PDO fetchObject
- <?php
- $dbHost = 'localhost';
- $dbUser = 'root';
- $dbPass = '';
- $dbName = 'fetchobject';
- $pdo = new PDO('mysql:host=' . $dbHost . ';dbname=' . $dbName, $dbUser, $dbPass,
- [
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
- PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
- PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,
- ]
- );
- trait Magic
- {
- private $data = [];
- public function __set(string $name, $value)
- {
- $this->data[$name] = $value;
- }
- public function __get($name)
- {
- return $this->data[$name] ? $this->data[$name] : null;
- }
- public function __isset($name)
- {
- return isset($this->data[$name]);
- }
- public function __unset($name)
- {
- unset($this->data[$name]);
- }
- public function __call($name, $arguments)
- {
- $action = substr($name, 0, 3);
- $property = strtolower(substr($name, 3));
- switch ($action) {
- case 'get':
- return $this->$property;
- break;
- case 'set':
- $this->$property = $arguments[0];
- break;
- default :
- return false;
- }
- }
- }
- class User
- {
- use Magic;
- public function FIO()
- {
- return $this->name . ' ' . substr(ucfirst($this->subname), 0, 1);
- }
- }
- $sql = "SELECT * FROM `users`";
- $users = $pdo->query($sql);
- while($user = $users->fetchObject('User')) {
- #echo '<pre>' . print_r($user, 1) . '</pre>';
- echo $user->FIO() . '<br>';
- echo $user->name . '<br>';
- echo $user->getAge() . '<br>';
- }
Пример
бд
бд
- -- phpMyAdmin SQL Dump
- -- version 4.6.5.2
- -- https://www.phpmyadmin.net/
- --
- -- Хост: 127.0.0.1:3306
- -- Время создания: Июл 16 2017 г., 19:17
- -- Версия сервера: 10.1.19-MariaDB
- -- Версия PHP: 7.1.0
- SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
- SET time_zone = "+00:00";
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8mb4 */;
- --
- -- База данных: `fetchobject`
- --
- -- --------------------------------------------------------
- --
- -- Структура таблицы `users`
- --
- CREATE TABLE `users` (
- `id` int(11) NOT NULL,
- `name` varchar(255) DEFAULT NULL,
- `subname` varchar(255) DEFAULT NULL,
- `age` int(2) DEFAULT NULL,
- `sex` enum('male','famale') NOT NULL DEFAULT 'male',
- `site` varchar(255) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
- --
- -- Дамп данных таблицы `users`
- --
- INSERT INTO `users` (`id`, `name`, `subname`, `age`, `sex`, `site`) VALUES
- (1, 'Vasya', 'Vasin', 32, 'male', 'http://test.com'),
- (2, 'Nikolay', 'Nikolayev', 33, 'male', 'http://testus.ru');
- --
- -- Индексы сохранённых таблиц
- --
- --
- -- Индексы таблицы `users`
- --
- ALTER TABLE `users`
- ADD PRIMARY KEY (`id`);
- --
- -- AUTO_INCREMENT для сохранённых таблиц
- --
- --
- -- AUTO_INCREMENT для таблицы `users`
- --
- ALTER TABLE `users`
- MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
- /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
- /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;