Object Access



In ZN Framework, objects (Library, Model, Controller, Command) are usually accessed in a static type. Besides, there are several other ways.

 

 

# Section Headings


# Static Access

# Static Conversion

# In-Class Access

# Access in Views [5.8.5]

# Parametric Access [5.7.7]

# Access in Views

 

 

# Static Access


All libraries of ZN Framework are accessed in static form. There is no restriction on access with this method. If the classes to be accessed do not have the same namespace as the OOP rule, they must be specified just below the use and namespace definition before calling. Otherwise, you will encounter an error like "Class 'ClassName' not found".

File: Controllers/Home.php
<?php namespace Project\Controllers;

use DB;

class Home extends Controller
{
    public function main()
    {
         output( DB::persons()->result() );
    }
}
Static Conversion

ZN Framework offers 2 different ways to provide static access to non-static classes that you have developed personally;

Specifying the class names with the prefix internal.
Use of Facade constant.

internal Prefix

If you specify the name of the class you are going to create with the internal prefix starting, a static view of the class will be created. This static view is created in the respective project's Resources/Statics/ directory. Using namespaces does not prevent static access.

File: Libraries/MyClass.php
<?php 

class InternalMyClass
{
    public function run()
    {
         # Your codes...
    }
}

After the definition above, you can use your class as follows.

File: Controllers/Home.php
<?php namespace Project\Controllers;

use MyClass;

class Home extends Controller
{
    public function main()
    {
         echo MyClass::run();
    }
}

Although the MyClass::run() method is not defined as static, access to this method is performed statically.

Facade Constant [5.7.9]

If the class you will create contains a const front = 'ClassName' definition, a static (facade) view of your class will be created. The static view is created in the directory containing the original class.

Warning: If the facade class and the original class do not have different namespaces, be careful not to use the same name for both classes.
File: Libraries/MyClass.php
<?php 

class MyClass
{
    # Make sure the facade class name differs from classes 
    # that do not contain namespaces or have the same namespace.
    const facade = 'My'; 

    public function run()
    {
         # Your codes ...
    }
}

After the definition above, you can use your class as follows.

File: Controllers/Home.php
<?php namespace Project\Controllers;

use My;

class Home extends Controller
{
    public function main()
    {
         echo My::run();
    }
}
Important: Our preference is to provide static access by using the facade constant, since it does not require a change in the original class name.

Difference Between Definitions

Although both of the above definitions allow you to access a non-static class statically, there are some differences between them. Even if these differences are not intended for use, they are important differences to be aware of.

  internal Facade
File Name The name of the class changes. The name of the class does not change.
Namespace It is not affected by namespace usage. Affected by namespace usage.
File Naming It is named in the form InternalClassName.php. It is named in the form of ClassNameFacade.php.
File Location Resources/Statics/ It is located in the same place as the original class.

 

 

# In-Class Access


In ZN Framework, you can access classes statically or in controllers with the $this object. If the class you want to access contains namespace, it does not prevent access with $this object. In other words, you can access using only the class name without specifying the namespace. As an OOP rule, classes do not need to be defined under the namespace with use when accessing $this.

File: Controllers/Home.php
<?php namespace Project\Controllers;

class Home extends Controller
{
    public function main()
    {
         output( $this->my->run() );
    }
}
Access in Views [5.8.5]

You can access the $this object in views using the template wizard.

File: Views/Home/main.wizard.php
{{ $this->my->run() }}
Warning: This usage is only valid for views using the template wizard.

 

 

# Parametric Access [5.7.7]


You can access the parameters of the controller methods by injecting objects.

File: Controllers/Home.php
<?php namespace Project\Controllers;

use My;
use DB;

class Home extends Controller
{
    public function main(My $my, DB $db)
    {
         output( $my->run() );
    }
}
Access in Views

You can access the class instances created with the parameter in the view with the same name.

File: Views/Home/main.wizard.php
{{ $my->run() }}