How to merge classes & call it's variables?

Discussion in 'PHP' started by HungryMinds, Jun 28, 2011.

  1. #1
    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)
    
    Code (markup):
    * 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;
    	}
    }
    
    Code (markup):
     
    HungryMinds, Jun 28, 2011 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    are you talking about class inheritance??????

    class B extends A
     
    bartolay13, Jun 28, 2011 IP
  3. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #3
    Yes Sir...

    First I Want To Set Index.php Variables In class A

    Then I Want Call To Those Variables In class B From class A.

    Is It Possible?
     
    HungryMinds, Jun 28, 2011 IP
  4. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Like This?
     
    class A
    {
    	public function f_A($get_id, $get_name, $get_email)
    	{
    		$set_id 	= $get_id;
    		$set_name	= $get_name;
    		$set_email	= $get_email;
                    print $set_id . ", " . $set_name . ", " . $set_email;
    	}
    }
    
    class B extends A
    {
    	function f_B()
    	{
    	$a = new A;	
                   // I want to use index.php file variables here.
    		$a->f_A($get_id, $get_name, $get_email);
    	}
    }
    
    PHP:
     
    The Webby, Jun 28, 2011 IP
    sar420 likes this.
  5. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    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 Class
    
    c_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 Class
    
    c_Element::f_URL();
    
    c_Element::f_IMG();
    Code (markup):
    * 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\" />";
    	}
    }
    Code (markup):
    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.
     
    HungryMinds, Jun 28, 2011 IP
  6. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    I like how you try to learn to optimize your code. Here 's something to get you started!

    
    class example_storage {
        public $var1;
        public $var2;
        public $var3;
    
        function __construct ($var1, $var2, $var3) {
            $this->var1 = $var1;
            $this->var2 = $var2;
            $this->var3 = $var3;
        }
    }
    
    class demo {
        function test ($data) {
            echo "Var1: ".$data->var1.", Var2: ".$data->var2.", Var3: ".$data->var3";
        } 
    }
    
    $data = new example_storage("test1", "test2", "test3");
    $mydemo = new demo();
    $mydemo->test($data);
    
    Code (markup):
    If you only need one instance, you can do it easier with static. But this is more generic.
     
    jazzcho, Jun 30, 2011 IP
  7. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #7
    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');
    Code (markup):
     
    HungryMinds, Jun 30, 2011 IP
  8. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Part 1

    OOP primer

    Think of a class as a design. (e.g. a design of a car). Now the class does NOT exist, just like the design of a car is not a car. To create an instance of a class (to create a car from a car design, you need the NEW keyword.

    $instance = new MyClass();

    When PHP executes the above line, in the backround it automatically runs the __construct() method.

    So you need $mysettings = load_settings($product, $id);

    Now you have an object called $mysettings which was produced from a design called load_settings.

    This should clear it up a bit.

    What the :: is.

    Back to the car design (the class). Sometimes, on the drawing you can have some notes. You only have 1 drawing (per class). So, you can read the notes of the drawing without having to create a car. This is generally called static access. The :: does that. You call the function directly from the car design and not from the car object. Sometimes, it is better, sometimes not. For now, always create the car. When you get more experienced, use ::.

    Part 2

    Take a look at the load_settings class and at my previous example. Your $set_product and $set_id variables are local to the function. See my example and fix it!!!!! You have more errors. I will not fix them for you, study my example and post your fixes.
     
    jazzcho, Jun 30, 2011 IP