The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-OOP - Array from parent class to subclass
Discuss Array from parent class to subclass in the PHP Development forum on Dev Shed. Array from parent class to subclass PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 24th, 2012, 05:40 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 55 m 2 sec
Reputation Power: 0
|
|
|
PHP-OOP - Array from parent class to subclass
Hi!
look this simple example:
class myClass {
var $lang = 'en';
var $translations = array();
public function __construct($lang,$translations = array()) {
$this->lang = $lang;
$this->array_translations = $translations;
}
public function foo() {
echo $this->lang;
echo $this->array_translations['2'];
}
}
class myClass2 extends myClass {
public function __construct() {
}
public function foo2() {
echo $this->lang;
echo $this->array_translations['2'];
}
}
$class = new myClass($lang,$translations);
$class2 = new myClass2();
$class2->foo2();
Why array_translations doesn't read in class myClass 2? Lang yes.. but array elements no? I don't understand 
|

November 24th, 2012, 07:29 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Because
1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments.
|

November 24th, 2012, 08:17 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
this is a misunderstanding of how objects work. The $translations property is an instance property, so every object has its own $translations. Setting the value for a particular object has no effect on the other instances that might exist at this point of time.
Think of a simpler example: If create a Mammal with the name "Peter" (could be a human, a dog, a cat, whatever), does that mean that all Humans (which are a subclass of Mammals) should now be named "Peter"?
|

November 24th, 2012, 12:04 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 55 m 2 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by requinix Because
1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments. |
Thanks for your answer.
I'have my array included in all page of my site:
$translations = array();
$translations['1'] = 'text1';
$translations['2'] = 'text2';
$translations['3'] = 'text3';
$translations['4'] = 'text4';
$translations['5'] = 'text5';
$translations['6'] = 'ecc';
I would like include this array element in my Class for being used in every subclass.
Is it possible?
|

November 24th, 2012, 12:44 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
If the array is known from the beginning, use a static property and set the array as the default value:
PHP Code:
<?php
class A {
static $x = array(1, 2, 3);
}
class B extends A {
function showX() {
var_dump( self::$x );
}
}
$b = new B();
$b->showX();
This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.
By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
PHP Code:
$x = array(
'a'
, 'b'
, 'c'
);
Last edited by Jacques1 : November 24th, 2012 at 12:47 PM.
|

November 24th, 2012, 01:02 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 55 m 2 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 If the array is known from the beginning, use a static property and set the array as the default value:
PHP Code:
<?php
class A {
static $x = array(1, 2, 3);
}
class B extends A {
function showX() {
var_dump( self::$x );
}
}
$b = new B();
$b->showX();
This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.
By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
PHP Code:
$x = array(
'a'
, 'b'
, 'c'
);
|
Ok, but my array is external... is included in all pages, and contain more elements.
The problem is setting external array into static element. 
|

November 24th, 2012, 04:01 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Then use a setter method.
PHP Code:
<?php
class A {
private static $x;
static function getX() {
return self::$x;
}
static function setX($x) {
self::$x = $x;
}
}
class B extends A {
function showX() {
var_dump( self::getX() );
}
}
$translations = array(
'foo'
, 'bar'
);
A::setX($translations);
$b = new B();
$b->showX();
|

November 25th, 2012, 04:06 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 55 m 2 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Jacques1 Then use a setter method.
PHP Code:
<?php
class A {
private static $x;
static function getX() {
return self::$x;
}
static function setX($x) {
self::$x = $x;
}
}
class B extends A {
function showX() {
var_dump( self::getX() );
}
}
$translations = array(
'foo'
, 'bar'
);
A::setX($translations);
$b = new B();
$b->showX();
|
Many thanks Jacques1!
you have been useful 
|

November 25th, 2012, 04:16 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 5
Time spent in forums: 55 m 2 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by gino8 Many thanks Jacques1!
you have been useful  |
Ps it's strange that you have to assign the static element out of the classroom 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|