ComputersProgramming

PHP construct: creating instances of classes

The idea of object-oriented programming is much broader than the capabilities of PHP because of its specifics, but even in the existing implementation it gives the programmer unlimited possibilities. The PHP construct is a special method of a class (object), which is called every time an instance of the class is instantiated.

The limitation is that PHP works at the moment of page formation. At the moment when the page is updated or another page of the same site is loaded, the necessary system of objects is formed again from scratch.

Creating an instance of a class

A class description is not required to have a constructor. If you need to prepare the initial values of variables, catch the instant of instantiation of the class (object), perform certain actions on other objects, then you can not use the PHP class construct syntax and write the appropriate code outside the class methods.

According to the logic of the object-oriented programming style, each class must have a constructor, moreover, it is necessary to start the class tree from the most abstract (absolutely empty) ancestor. It is a relic of the past, and not a sensible practice.

When the pedigree starts with a meaningful class that has its own data and properties associated with external data (objects), function routines PHP can not be dispensed with.

In this example, when you create (PHP construct) an instance of the date class, this function (the constructor) is called. It has a specific name __construct and is called by the automaton only once, when an instance of the class is created.

This class provides a static variable $ iUniqueNo, which in each new instance of this class will have a unique value. Instances of classes have nothing in common except descriptions within the syntax of PHP and the developer's intended interaction of their methods.

Inheritance of initialization logic

Each object must realize its purpose, have what it needs, and do what it should. From such a reasonable point of view, initialization at each level of the pedigree can include initialization in each ancestor called from the descendant level.

In this example, the keyword parent :: allows you to call the parent's constructor from the descendant's level. Semantics is simple. First, the ancestor must perform its initialization, then the current instance. The first follows its logic, the second - its own.

When each object is engaged in its own business, the overall process looks correct and understandable. But this rule should not be considered the norm for all systems of objects.

The pedigree of the food system in the first approximation may have something in common, but products of milk, watermelon, pasta and cereals, although they refer to such a system, but look and describe quite differently.

The developer must build each system of objects from the field of application, and not from how it was once suggested by someone. Each task is unique, the requirement to use it in PHP parent construct is not absolute.

General and private constructors

By default, the constructor is generic and is available for use by all descendants. It is not necessary to specify public function construct, PHP by default considers all described, as the general.

How much does it make sense to use the keyword private when describing constructors - the specificity of the task, the feature of the development process or the preferences of the programmer?

From the conceptual point of view, the pedigree of objects can allow any prohibitions on ancestors relative to descendants, but how much is reasonable logic to say is difficult, in any case, in a general context.

Object lifetime

The concept of object-oriented programming is broader than PHP construct class for the simple reason that the latter exist only at the moment of page formation, its re-creation or creation of another page of the site.

Participation of AJAX through JavaScript browser and proper code on the server will help prolong the life of objects, but in any version it will be a limited style.

PHP provides the ability to execute a script on the server when the client "disconnected" and "allow" the client back into the script it ran earlier, but this is not the option when the object-oriented program is implemented in C ++.

In the latter case, you can build a complete system of objects, which will exist "forever", while the program is running and running. However, this is the only thing that a stationary programming language like C ++, C #, Pascal & Delphi can boast of. In a dynamic Internet world, everything is built differently, lives faster and achieves more.

From serialization to self-preservation

You can find a historical justification for the term "serialization" and the appearance in everyday life of the concept of "magic methods." But everything is much simpler. Exactly the same as the freedom of C ++ differs from the rigidity of C #, serialization differs from banal concepts:

  • Write an object to a string;
  • Read the object from the string.

To surround said magic with mythical magical methods is beautiful, sonorous, but not very practical. The world of information is interesting first of all in that everything visible, audible and tangible can be described by a simple and consistent text.

Information has always been, is and will be a string of symbols. It does not matter what kind of nature. In the formal constructions of programming languages, the nature of the characters is one - the encoding table.

The idea to turn an object into a string so that you can restore it from this line without loss of essence, if necessary, is a very practical idea.

From self-preservation to self-development

The semantics of the construct constructor within its syntax is limited, but if the designer develops from a developing position:

  • There is a beginning - a completely new instance is created;
  • There is a current state - an existing instance is created.

Limitations of PHP because the system of objects on it exists only at the moment of page formation, will take off by themselves.

Creating a system of objects when creating a page of the site, you can save it. For simplicity, this process does not have to be called serialization, you can just keep the current state of things (database, files), and when you need to re-create the same page or create another one on the same site, just restore the current state of things that has already been formed.

In this context, the system of objects is created only once, and in the process of the site it simply develops. With such a scheme, you can design a system of objects as something that adapts to changing conditions of existence.

A self-saving system of objects "remembers" the visitor's actions and the state of the pages, and every time PHP is started, it is not created from scratch, but is restored in the previous state.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.