Adding functions to an object

Adding functions to an external file

Page 1 of 1

8 Replies - 1119 Views - Last Post: 18 July 2009 - 01:01 PM Rate Topic: ***** 1 Votes

#1 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Adding functions to an object

Posted 17 July 2009 - 05:55 PM

My only alternative solution is to write a file with a class and properties, and then to read whatever is added to the properties from an external file. However, I'd rather add a function or property to a class comprised within an external file. To clarify, I'd rather do this...
page_a.php
class pagea {

 public $vara = "value";

 public function testa() {
   print $this->vara;
 }
}
page_b.php
//add new property and function


As opposed to:
$file = fopen("file.php", "w");
fwrite($file, "<?php class pageb { public $varb = 'value2'; } ?>";


Note: just imagine escapes for the fwrite

This post has been edited by livelonger87: 17 July 2009 - 05:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding functions to an object

#2 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Adding functions to an object

Posted 17 July 2009 - 06:01 PM

As a second alternative, I'd also rather reset the defaults of properties via external files (e.g. to set the value of $vara from the page_b.php) - I'm unsure how to change the defaults. Any help is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#3 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Adding functions to an object

Posted 17 July 2009 - 10:21 PM

very simple
$obj = new pagea;
$obj->vara = 'your value';
echo $obj->vara;


Was This Post Helpful? 0
  • +
  • -

#4 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Adding functions to an object

Posted 18 July 2009 - 01:21 AM

View Postnoorahmad, on 17 Jul, 2009 - 09:21 PM, said:

very simple
$obj = new pagea;
$obj->vara = 'your value';
echo $obj->vara;


Thanks, but this won't change the default values. I mean, I want to change the default value of vara so that when I recall the value from another page, it displays the value I changed it to rather than its default. (The script resets vara to its initial value - I want to permanently change its default value)
Was This Post Helpful? 0
  • +
  • -

#5 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Adding functions to an object

Posted 18 July 2009 - 01:53 AM

ok lets say you are calling you class form one pageone.php and assigning value to vara, then you want to call it from page2.php and you want to pring the vara value lets see it practical
class.php
class n
{
	public $vara = 'value';
	
	function a()
	{
		echo $this->vara;
	}	
}



i want to call it in page1.php
$obj = new n;
$obj->vara = 'your value<br />';
echo $obj->vara;


and from page2.php
$obj1 = new n;
echo $obj1->vara;


when we want to call and class we need to create a new object $obj = new objectname; in every page when we are using new object it is making a new class with it's default values.

this class will help you
class cls
{
	function one()
	{
		echo 'one';
	}
	function two()
	{
		echo 'two';
	}
}

cls::one();
cls::two();


hope you know what i mean.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Adding functions to an object

Posted 18 July 2009 - 06:23 AM

It's hard to understand what you're asking, but I think that you've got a class defined in one file that you want to extend in another:

Original class in page_a.php:
class pagea {

 public $vara = "value";

 public function testa() {
   print $this->vara;
 }
}


New file:
require("page_a.php");
class pageb extends pagea {
	public $vara = "New Value";
}

$obj = new pageb;
$obj->testa();


Was This Post Helpful? 0
  • +
  • -

#7 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Adding functions to an object

Posted 18 July 2009 - 06:30 AM

Basically, this is the entire point of the extends (whatever you call it)

See here, example class:
class test {
	var $defVal = true;
	var $defVal2 = true;
	var $defVal3 = true;
	
	function echoVals(){
		var_dump($this->defVal);
		print "<br>\n";
		var_dump($this->defVal2);
		print "<br>\n";
		var_dump($this->defVal3);
	}
}


And here we overwrite the default values:
class testChangeDefault extends test {
	var $defVal = false;
	var $defVal2 = false;
}


Then we call it and output the data:
$class = new testChangeDefault();
$class->echoVals();


Returns:

Quote

bool(false)
bool(false)
bool(true)


This can be done with overwriting functions, variables, whatever...and you keep the things you didn't change as if you never changed them.

Hope this helps.

[Edit] Damn you, CT...damn you...

Yours,
Shane~

This post has been edited by ShaneK: 18 July 2009 - 06:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 livelonger87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 27-May 08

Re: Adding functions to an object

Posted 18 July 2009 - 12:53 PM

Thank for all the replies! Extends didn't work for what I was going. To clarify, let's consider that we have... a class with all page functions, within which an installation function exists. When it detects the user is upon a certain stage - a stage where they're requested to submit their server details, then if submitted, check posts and if they're fine, then change the default values of properties in another class called mysql__connect. I tried embedding extends into the installation function but it doesn't work - are there any other areas in PHP that would allow for this to be possible?
Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty   User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: Adding functions to an object

Posted 18 July 2009 - 01:01 PM

am i right in thinking that you want to be able to change the value of some variables in your script permanantly without having to load up the script or rewrite it ?


if so couldnt you stor the value in the db and then do something like this

page_a.php
class pagea { 
 public $vara;

 public function testa() {
   print $this->vara;
 }

 function get_vara(){
   $value = mysql_result(mysql_query(/*mysql query*/), 0);
   return $value;
 }
}
page_b.php
//add new property and function




and then call

$obj = new pagea;
$obj->vara = $obj->get_vara();
$obj->testa();




untested so dont know how it will work. think thats what you want
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1