Showing posts with label Private. Show all posts
Showing posts with label Private. Show all posts

Object Oriented Programming in JavaScript

There are several ways to do this, such as using prototype and JavaScript object. However, simplest may be the best. Following method matches the orthodox structure of OO, which is put everything inside of a class file:

; var ObjectClass = function(inputPara)
{
; var privateAttribute_1 = inputPara
; this.publicAttribute_Temp = 200
; function privateMethod_SetInternally()
{ ; privateAttribute_1 = 100
}
; this.publicMethod_1 = function()
{ ; privateMethod_SetInternally()
; return 50*privateAttribute_1
}
; this.publicMethod_Get = function()
{ ; return privateAttribute_1
}
; this.publicMethod_SetExternally = function(valueSetLater)
{ ; privateAttribute_1 = this.publicMethod_1*valueSetLater*123456789
}
}

Please note, for private method, do not use Varibalized Function, which is also called Function Literal, Function Reference or Function Pointer, like this:

; var privateMethod_SetInternally = function () {…}

Indeed, private method privateMethod_SetInternally is useless here, because you can always set internally directly:

; privateAttribute_1 = newValue

The purpose of privateMethod_SetInternally here is to show how to call this method, as stated in publistMethod_1. There is no way for outsider to use this private method, but through publistMethod_1.

All attributes/method started with this. is the public accessible by outsider caller, or the object based on this Class.

To use this Class, an object needs to be created:

; var obj = New ObjectClass(300)
; var att1_inThere = obj.publicMethod_Get()
; obj.publicMethod_SetExternally(400)

Please note, internally, calling all public accessible attributes/methods would not need with (), while private methods need to include (). However, for outsider object, public methods need to include () as well, like above.

There is a problem. Since internally calling public methods could not be with (), there is no way to pass the parameter while calling. In here, publicMethod_SetExternally can be called and assigned with parameters externally with no problem, but can only be called without assigning parameter internally. In this case, parameter is essential, so it would generate “not defined” error.

To overcome this, publicMethod_SetExternally needs to be rewritten as follows:

; this.publicMethod_SetExternally = function()
{ ; privateAttribute_1 = this.publicMethod_1*this.publicAttribute_Temp*123456789
}

Internally, it can now be called directly:

; this.publicMethod_SetExternally

Externally, it would be called like this:

; obj.publicAttribute_Temp = 400
; obj.publicMethod_SetExternally()

Fatal error: Call to undefined function... In PHP

First of all, the information contained in this error message is correct. The error was caused by the caller unable to access the called function, thus regarded as undefined function. There are several possibilities:

1. The function called is located in different PHP file, and it is not included or required by the caller file.

2. The php built-in function called has been deprecated.

3. Having an issue of encapsulation. It often happened when caller itself is not instantiated, and tried to call the function directly. Even if both caller and called are in same class, the PHP rule of encapsulation still applies. To fix up this, the called function must be declared as static, and the calling method need to be:

CalledClass::CalledFunction(); (Method 1)

If it is located in same class of the caller, it can be:

self::CalledFunction(); (Method 2)

When the caller is the object or instantiated of the class, it is necessary to use dynamic way, though it may be a public static function:

$result = $this->calledFunction(); (Method 3)

It would become more complicated when the caller is an function located within an instantiated object. In this case, method 3 does not work. Rather, Method 2 should be used.

Summary:

1. When caller is instantiated and externally called dynamically:
Called function declaration must be public.
Calling method: $instantiatedObject->calledFunction();

2. When caller is instantiated and externally called, and called function declared as static:
Called function declaration would be public static.
Calling method: $instantiatedObject->calledFunction();

3. When caller is not instantiated and externally called:
Called function declaration must be public static, or public when function is not within the class.
Calling method: $className::calledFunction(); or calledFunction(); when function is not within the class.

4. When caller is part of constructor and use dynamic method:
Called function declaration should be: private
Calling method: $this->calledFunction();

5. When caller is part of constructor and called function declared as static:
Called function declaration would be: private static
Calling method: $this->calledFunction();
This is not recommended.

6. When caller is internally located in another function, and called function declared as static:
Called function declaration should be: private static
Calling method: self::calledFunction();

There should be no other circumstances.


http://stackoverflow.com/questions/2220809/calling-a-method-from-another-method-in-same-php-class

Public, Private, Protected, Static Function

Public - Available to everyone who can access the class. If a function without declared as private or protected, it is public. In PHP, when variable is declared with keyword 'var', it is declared as public.

Private - Available only within the class where the function/variable is declared.

Protected - Available only within the class, or any class that is derived from the class, or called inherited.

Dynamic - Can only be called after create an object (instantiation) from the class. If a function without declared as static, it is dynamic.

Static - Can be called without creating an object (instantiation) from the class.


http://www.daniweb.com/forums/thread285410.html
http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php

Labels