Oracle Consulting Oracle Training Oracle Support Development
Home
Catalog
Oracle Books
SQL Server Books
IT Books
Job Interview Books
eBooks
Rampant Horse Books
911 Series
Pedagogue Books

Oracle Software
image
Write for Rampant
Publish with Rampant
Rampant News
Rampant Authors
Rampant Staff
 Phone
 800-766-1884
Oracle News
Oracle Forum
Oracle Tips
Articles by our Authors
Press Releases
SQL Server Books
image
image

Oracle 11g Books

Oracle tuning

Oracle training

Oracle support

Remote Oracle

STATSPACK Viewer

Privacy Policy

 

   
 

ss

Oracle Tips by Burleson 

Classes and Methods

PHP allows OO (Object Oriented) programming much in the same way as C++ or Java.  This book will cover the object model for PHP5 as it is much more complete than the one in PHP4, which is gradually being phased out. 

Object oriented programming is based on the idea that related variables and methods used to access variables are placed together in entities called classes. Classes can be thought of as new data types, equivalent to strings or numbers. Classes have members and methods. Members are variables and methods are functions. The following is an example of a class:

Example 11:

#!/usr/local/bin/php
<?php
    class example {
        private $val;
        public function printval() {
                echo $this->val,”\n”;
                }                                                                               

        public function getval() {
           return($this->val);
        }
        public function setval($x) {
           $this->val=$x;
        }
        function __construct($x) {
           $this->val=$x;
        }
   }
$a=new example("This is an example class!");
$a->printval();
?>

When executed, this example prints out text:

    $ ./example11.php
        This is an example class!

So, what does this class consist of? This class has a private member $val. What does the word “private” mean? Private means that the member is not accessible directly by functions that do not belong to the class “example”. If an attempt is made to add the following line of code to example11.php, an error will occur: 

$a->val="This will cause an error";

The execution shows that there is something wrong:

$ ./example11.php
        This is an example class!
PHP Fatal error:  Cannot access private property example::$val in /home/mgogala/work/PHP/example11.php on line 21

Member $val is private and can only be accessed by the methods of the class example. Member $val could be declared as public, in which case, there would be no problem with the assignment above. The method can also be declared protected, which means that it can be accessed only by the methods of the class “example” and the classes that inherit from the class example. This is an important distinction: classes that extend “inherit from” class “example” cannot access the private members of the original class.  Inheritance will be discussed in more detail later.

The variable $this, which points to the current variable of the given class should also be noted. It performs the same function as the word “me” in the English language; it points to ourselves. In PHP, the “me” is implemented as the variable $this.

Another thing to note are the getval() and setval() methods. This is the recommended practice in OO programming; always to have the data member private and always create get/set access methods. Of course, this is a practice and not a religion.

Sometimes there are valid reasons for having public members. Any programming practice or principle can lead to suboptimal and ugly programs when turned into a religion. Ultimately, the programmer is the person to make the call. Any serious program will have areas in which general recommendations have been sacrificed to achieve greater code clarity or performance. All that is required is that those trade-offs are made conscientiously and not as a result of ignorance.

The last thing to note is the existence of the __construct function.  This function is called “constructor”. It is not declared as public, protected or private, which means that it is public. Constructor is invoked with the operator new. Operator new constructs (genuine OO term would be “instantiates”) a variable of type “example” (classes are new data types).

There is also a __destructmethod although there is no delete operator. If so, when does the __destruct method get executed? The __destruct method gets executed when a variable belonging to the class example falls out of the execution scope and there are no live references to that variable. Now, few remarks for the users of PHP4 who have not yet used PHP5:

  • Constructor in PHP4 was a function with the same name as the class in which it constructs. For compatibility reasons this type of constructor is still recognized, but deprecated.

  • PHP4 did not have member protection. Classes were defined by using the keyword var which is synonymous with the keyword public, again for compatibility reasons.

  • PHP4 did not have destructors.


See code depot for complete scripts


The above book excerpt is from:

Easy Oracle PHP

Create Dynamic Web Pages with Oracle Data

ISBN 0-9761573-0-6   

Mladen Gogala

http://www.rampant-books.com/book_2005_2_php_oracle.htm

 

Download your Oracle scripts now:

www.oracle-script.com

The definitive Oracle Script collection for every Oracle professional DBA

Linux Oracle commands syntax poster

ION Oracle tuning software

Oracle data dictionary reference poster



Oracle Forum

BC Oracle consulting support training

BC remote Oracle DBA   

 

   

 Copyright © 1996 -2017 by Burleson. All rights reserved.


Oracle® is the registered trademark of Oracle Corporation. SQL Server® is the registered trademark of Microsoft Corporation. 
Many of the designations used by computer vendors to distinguish their products are claimed as Trademarks
 

Hit Counter