Jump to content

How to merge classes & call it's variables?


HungryMind
 Share

Recommended Posts

Hi!I Have Two Classes & I Want To Merge Both Classes & Want To Use It's Variables.Please Guide.* index.php

require('config.php');$id = 'abc';$name = 'def';$email = 'ghi';A::f_A($id, $name, $email)

* config.php

class A{	function f_A($get_id, $get_name, $get_email)	{		$set_id 	= $get_id;		$set_name	= $get_name;		$set_email	= $get_email;	}}class B{	function f_B()	{		// I want to use index.php file variables here.		print $set_id . ", " . $set_name . ", " . $set_email;	}}

Link to comment
Share on other sites

Why exactly do you need it this way? This is not a good practise in programming.You're attempting to use global variables. Usually if you're using global variables you should not have a class or function at all.

Link to comment
Share on other sites

Why exactly do you need it this way? This is not a good practise in programming.You're attempting to use global variables. Usually if you're using global variables you should not have a class or function at all.
Thanks For Reply.Actually I've Code Like This:* index.php
require('config.php');$var1 = 'a';$var2 = 'b';$var3 = 'c';// Only First Time I Want To Send All These Variables To Classc_Default::f_Default($var1, $var2, $var3);// I Can Send All My Variables From This URL Class// But I Want To Use These Defined Variables From c_Default Classc_Element::f_URL();c_Element::f_IMG();

* config.php

class c_Default{	function f_Default($action)	{		$set_var1 = $var1;				$set_var2 = $var2;				$set_var3 = $var3;	}}class c_Element{		function f_URL()	{			   // I Want Use Those Variables Here From c_Default Class  			   // I can Use "global" Here, But! In Every Function I Have To Make Those Variables Global			   print "<a href=\"http://www.abc.com/index.php?var1=$set_var1&var2=$set_var2&var3=$set_var3\">Click Here</a>";	}		function f_IMG()	{			   // Once Again I Want To Use All Those Variables From c_Default Class			   // And I Don't Want To Make Those Variables "global"			   print "<img src=\"http://www.abc.com/$set_var1.png\" alt=\"$set_var2\" title=\"$set_var3\" />";	}}

All This Process I Can Create Using "global", But Someone told me that "global" is not a good practice if i'm making variables global again & again.Please Guide.Thanks a Lot.

Link to comment
Share on other sites

Thanks For Reply Friends.Please Read All Code Comments.

$product = 'ebook';$id		  = '100';load_settings::__construct($product, $id); // How Can I Call __construct Here?class load_settings{	 public function __construct($get_product, $get_id)	 {		  $set_product = $get_product;		  $set_id		  = $get_id;	 }}class create_elements extends load_settings{	 function create_url($keyword, $url)	 {		  // I Want Use $set_product as 'ebook' & $set_id as '100' From __construct.		  // How Can I Call Variables From __construct function?		  // Look $set_product & $set_id Here, I Want To Call These Variables Here From __construct.		  print "<a href='http://www.abc.com/product.php?product=$set_product&id=$set_id'>$keyword<a>';	 }}Link 1: create_elements::create_url('Harry Potter');

Edited by HungryMind
Link to comment
Share on other sites

You don't use constructors with static classes, normally the constructor gets called when you create a new instance:$obj = new load_settings();You also need to use the $this keyword if you want to set properties of the object.

	 public function __construct($get_product, $get_id)	 {		  $this->set_product = $get_product;		  $this->set_id		  = $get_id;	 }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...