Core: How to implement my own helper functions safely?

Created on 3 May 2017  ยท  12Comments  ยท  Source: php-telegram-bot/core

What is the best and safest way to implement my own functions? Should I include them in hook.php?
I want my functions to be able to use the current exiting classes (this plugin's) as well (like getMessage()).

I'm not much familiar with classes in programming.

Most helpful comment

Ah of course, because you'll have to add the namespace you used in your Helper.php file, which should be namespace Helpers;.

So in your StartCommand.php you need to put use Helpers\Helpers; (instead of use Longman\TelegramBot\Helpers;)

All 12 comments

Maybe this wiki article can help you:
https://github.com/php-telegram-bot/core/wiki/Snippets

I personally use composer autoloader with my own namespaces, putting all classes in one directory.

I'd personally go with @jacklul's suggestion too, to implement all your code in own classes.

I'm not much familiar with classes in programming.

This is why I thought of pointing you to the wiki ๐Ÿ˜Š

@jacklul, I don't have access to terminal with my hosting plan. Can I still use composer for that purpose?

I believe there were wrappers to use composer from webspace, you can always run composer locally and then upload vendor directory.

@jacklul thank you very much for helping.

@noplanman, I'm now more familiar with and interested in OOP. Can you give me some examples on how to implement my own helper class using composer with my own namespaces?

@ttvd94 Great to hear that you're getting more into OOP :tada:

To use the composer autoloader, I suggest to get a bit familiar with it by reading here:
https://getcomposer.org/doc/04-schema.md#autoload

Depending on how you want to manage your namespaces, you need to choose the specific autoloading method.

Give it a try and let me know if you need more help!

@noplanman I read the article and also watched some youtubes, but couldn't get the autoloader to work. I keep getting this error:
PHP Fatal error: Uncaught Error: Class 'Longman\TelegramBot\Helpers' not found in /path_to_bot/Commands/StartCommand.php:59

Here is my composer.json file:

{
    "name": "telegram-bot/project-name",
    "type": "project",
    "description": "Project description",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Helpers\\": "Helpers/"
        }
    },
    "require": {
        "longman/telegram-bot": "*"
    }
}

And here is my folder structure:

bot-root/
โ”œโ”€โ”€ commands/
โ”œโ”€โ”€ Helpers/
โ”‚   โ”œโ”€โ”€ Helpers.php
โ”œโ”€โ”€ vendor/
โ”œโ”€โ”€ composer.json
โ”œโ”€โ”€ composer.lock
โ””โ”€โ”€ hook.php

And StartCommand.php looks like:

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Helpers;
use Longman\TelegramBot\Request;

class StartCommand extends SystemCommand
{
    protected $name = 'start';
    protected $description = 'Start command';
    protected $usage = '/start';
    protected $version = '1.1.0';
    protected $private_only = true;

    public function execute()
    {
        $message = $this->getMessage();
        $helpers = new Helpers();
        return Request::sendMessage(['chat_id'=> $chat_id, 'text'=> $helpers->hey()]);
    }
}

And Helpers.php:
``` namespace LongmanTelegramBot;

class Helpers {
public function __construct() {}
public function hey() {
return 'heyy';
}
}
```

Ah of course, because you'll have to add the namespace you used in your Helper.php file, which should be namespace Helpers;.

So in your StartCommand.php you need to put use Helpers\Helpers; (instead of use Longman\TelegramBot\Helpers;)

@ttvd94 Can we close here then?

Sure. Thanks for your help @noplanman <3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

noplanman picture noplanman  ยท  3Comments

ttvd94 picture ttvd94  ยท  4Comments

sayjeyhi picture sayjeyhi  ยท  3Comments

abbe-cipher picture abbe-cipher  ยท  4Comments

sineverba picture sineverba  ยท  3Comments