Custom Class and Functions in Zend Framework

I am developing a break controller using the Zend Framework, and I need to use an external php class in one of my controller action methods. How can I add this class or access it from my controller action method?

Do I need to write a custom library?

In my controller action method, iam extracts information from a polygon from a database. Now I have an external php class that can check if a point is inside the polygon.

Google on this issue, I found that we can write our own library classes for this. But I'm not so sure about that. So I want to know how to incorporate this external functionality into my controller action. Please guide.

+3
source share
2 answers

The simplest approach is just a includeclass, for example

class PolygonController
    public function someAction()
    {
        include 'path/to/class/file.php';
        $pointLocation = new pointLocation;

        // do something $pointLocation;
    }

You may also be able to download this file using Zend_Loader or Autoloader, but since the class name does not comply with the Pear / ZF naming convention, the easiest is this. It doesn't matter where you put the actual class file as long as it is available.


Since this is a third-party class, you can extend it to conform to the ZF / Pear naming convention. Then put it in the library folder so you can use Autoloader:

<?php
include 'path/to/class/file.php';
class My_PointLoader extends pointLoader {}

APPLICATION_ROOT/lib/My/PointLoader.php

"My_"

$autoloader->registerNamespace('My_');

$pointLoader = new My_PointLoader; , .


, API , ,

<?php
include 'path/to/class/file.php';
class My_PointLoader_Adapter
{
    protected $_pointLoader;
    public function __construct(pointLoader $pointLoader = NULL)
    {
        if($pointLoader === NULL) {
            $pointLoader = new pointLoader;
        }
        $this->_pointLoader = new pointLoader;
    }
    public function someAdapterMethod($foo)
    {
        $this->_pointLoader->someMethodInPointLoader($foo);
    }
    // … more methods
}

, , Autoloader .

+4

:

  • Zend_Maths_Point_Location
  • zend Zend/Maths/Point
  • Location.php
  • :

    $Point = new Zend_Maths_Point_Location

Zend Autoloader .

Zend, ,

+5

Source: https://habr.com/ru/post/1794913/


All Articles