Page 39 - Open Soource Technologies 304.indd
P. 39

Unit 2: Language Basics



            $arName[“item1”] = “abc”;                                                             Notes
            $arName[“item2”] = “def”;
            $arName[“item3”] = “ghi”;

            Objects
            PHP is capable of functioning as an object-oriented programming language (or OOP). As such, it
            must be able to handle objects. An object is a data type that allows for the storage of not only data
            but also information on how to process that data. The data elements stored within an object are
            referred to as its properties, also sometimes called the attributes of the object. The information, or
            code, describing how to process the data compromises what are called the methods of the object.

            Objects have two components to their construction. First, you must declare a class of object. It
            defines the structure of the object to be constructed. Then you instantiate the object, which means
            you declare a variable to be of a certain class and assign values to it appropriately.
            Another way of looking at objects is that they allow you to create your own data types. You
            define the data type in the object class, and then you use the data type in instances of that class.
            Yes, there is also anis_object() function to test whether a variable is on object instance.
            2.2.8 Resources

            Many modules provide several functions for dealing with the outside world. For example, every
            database extension has at least a function to connect to the database, a function to send a query
            to the database, and a function to close the connection to the database. Because you can have
            multiple database connections open at once, the connect function gives you something by which
            to identify that connection when you call the query and close functions: a resource.
            Resources are really integers under the surface. There main benefit is that they are garbage collected
            when no longer in use. When the last reference to a resource value goes away, the extension that
            created the resource is called to free any memory, close any connection, etc. for that resource:
            $res = database_connect( ); // fictitious function database_query($res); $res = “boo”; // database
            connection automatically closed
            The benefit of this automatic cleanup is best seen within functions, when the resource is assigned
            to a local variable. When the function ends, the variable’s value is reclaimed by PHP:

            function search ( ) { $res = database_connect( ); $database_query($res); }
            When there are no more references to the resource, it is automatically shut down.
            That said, most extensions provide a specific shutdown or close function, and it is considered
            good style to call that function explicitly when needed rather than to rely on variable scoping to
            trigger resource cleanup.
            Use the is_resource( ) function to test whether a value is a resource:
            If (is_resource($x)) { // $x is a resource }
            2.2.9 Null

            There’s only one value of the NULL data type. That value is available through the case-insensitive
            keyword NULL. The NULL value represents a variable that has no value (similar to Perl’s undef
            or Python’s None):
            $aleph = “beta”; $aleph = null; // variable’s value is gone $aleph = Null; // same $aleph =
            NULL; // same




                                             LOVELY PROFESSIONAL UNIVERSITY                                    33
   34   35   36   37   38   39   40   41   42   43   44