note 107451 modified in keyword.extends by danbrown

From: Date: Thu, 10 May 2012 21:14:15 +0000
Subject: note 107451 modified in keyword.extends by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187857@lists.php.net to get a copy of this message
I have found this the best way to grab a parents variables and such (that I love to set in the constructor, like standardized table names, etc.). I just call the parents constructor in the child constructor, then change variables inside the child constructor that needs to be customized for that class, or just leave it alone to make sure I understand I am using the parents constructor in the child constructor. (it's a good note and practice of mine.).

<?php
class MyParentClass
{
 $this->table_one = "my_table";
 $this->table_two = "variable_table";
}

class MyChildClass extends MyParentClass
{
 public function construct()
 {
 parent::__construct();

 $this->table_two = "newly_defined_table";

 }
 public function action()
 {
 $table1 = $this->table_one;
 $table2 = $this->table_two;
 
 return $table1 . " - " . $table2;
 }
}

$myChildClass = new MyChildClass;

echo $myChildClass->action();

//Displays my_table - newly_defined_table
?> --was-- I have found this the best way to grab a parents variables and such (that I love to set in the constructor, like standardized table names, etc.). I just call the parents constructor in the child constructor, then change variables inside the child constructor that needs to be customized for that class, or just leave it alone to make sure I understand I am using the parents constructor in the child constructor. (it's a good note and practice of mine.). class MyParentClass { $this->table_one = "my_table"; $this->table_two = "variable_table"; } class MyChildClass extends MyParentClass { public function construct() { parent::__construct(); $this->table_two = "newly_defined_table"; } public function action() { $table1 = $this->table_one; $table2 = $this->table_two; return $table1 . " - " . $table2; } } $myChildClass = new MyChildClass; echo $myChildClass->action(); //Displays my_table - newly_defined_table http://php.net/manual/en/keyword.extends.php

« previous php.notes (#187857) next »