do you need to use global?
IMO you should use just 1 global variable, thats is what I call "entry point"

My scripts looks like...

require_once 'loader.php';
Loader::registerAutoload();
$foo = new Foo();
$foo->doStuff();

This way you can develop faster and do maintenance better avoiding
problems with third-party.

Here you have some rules for remember how to access

you want a $var from outside and you are outside an object or
function? => use the $var
you want a $var from outside you are inside an object or function? =>
global $var o $GLOBALS['vars'] (better to pass it as arg)
you want a $var from an object and you are inside the same object? =>
use $this->var (better $this->getVar() )
you want a $var from an object and you are inside other object? => use
$object->getVar() or Class::getVar()


It is a good practice to declare the object members as "protected" and
provide s/getters for each member (when your design allow it). Also
you can overload by using the __get, __set and __call
It is really easy to make an "automagic" object

Class AutoMagic
{
        protected $_vars = array();
        
        public/*mixed*/
        function __get(/*string*/$name)
        {
                return isset( $this->{ $name } ) ? $this->_vars[ 
strtolower($name) ] : null;
        }
        
        public/*mixed*/
        function __set(/*string*/$name,/*mixed*/$value)
        {
                return $this->_vars[ strtolower($name) ] = $value;
        }
        
        public/*boolean*/
        function __isset(/*string*/$name)
        {
                return array_key_exists( strtolower($name), $this->_vars );
        }
        
        public/*void*/
        function __unset(/*string*/$name)
        {
                if( isset( $this->{ $name } ))
                        unset( $this->_vars[ strtolower($name) ] );
        }
        
        public/*mixed*/
        function __call(/*string*/$method,array $args)
        {
                $type = strtolower( substr( $method, 0, 3 ) );
                $property = substr( $method, 3 );
                
                switch( $type )
                {
                        case 'get':
                                return $this->{ $property };
                        
                        case 'set':
                                if( !array_key_exists(0, $args) )
                                        trigger_error( 'Bad call in ' . 
get_class($this) . '::' . $method
.'. Method needs an argument' );
                                
                                return $this->{ $property } = $args[0];
                        
                        case 'has':
                                return isset( $this->{ $property } );
                        
                        case 'del':
                                unset( $this->{ $property } );
                                return;
                }
                trigger_error( 'Bad call in ' . get_class($this) . '::' . 
$method );
        }
}

On Tue, Jul 14, 2009 at 10:01 AM, Darren
Karstens<darrenkarst...@googlemail.com> wrote:
>> Oh and if one class uses methods in another class .... do I instansiate a
>> new object of the other class .... I have seen use of OtherClass::Method
>> ....  is this better method of $obj = new OtherClass()  use
>
> The :: is used to access static methods of a class. Static methods can
> be used without creating an instance of the class because they dont
> use any of the classes member variables.
> For example say you have a class with a function for calculating the
> area of a rectangle:
> class SomeMathFunctions {
>    public function calculateRectangle($width, $height) {
>        return $width*$height;
>    }
> }
>
> To use this function you would need to first create an instance of the
> class then call the method using the normal -> :
> $funcs = new SomeMathFunctions();
> $area = $funcs->calculateRectange(10,15);
>
> But if you create the function as static by using " public static
> function calculateRectangle($width, $height) { "
> then you can access the method by using just 1 call:
> $area = SomeMathFunctions::calculateRectange(10,15);
>
> So for creating utility functions its better to use static methods
> since you dont get the overhead of creating a new instance of the
> class.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



-- 
Martin Scotta

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to