Jump to content

Recommended Posts

Hi guys.

 

well i get that if i have

 

class a
{

function a()
{
//do something
}
}

class b extends a
{
}

 

then class b has access to all of class a's functions variables ect

 

but what if i have something like this:

 

class a
{

function a()
{
//do something
}
}

class b extends a
{

function b()
{
// do something
}
}

class c extends a
{
function b()
{
// do something
}
}

class d extends a
{
function b()
{
// do something
}
}

 

how would i access the different function b's?

Link to comment
https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/
Share on other sites

hmm im a bit lost, ok heres my code:

 

index.php

<?php
include("form.php");
$form->addvaltype("alphanumtest", "alphanum");
$form->addvaltype("digit test", "numeric");
print_r($form->valtype);
if($_POST)
{
$form->validate();
}
?>
<form action="" method="post"><br />
<input name="alphanumtest" type="text"><br />
<?php echo $form->error("alphanumtest"); ?>
<input name="digit test" type="text"><br />
<?php echo $form->error("digit test"); ?>
<input type="submit">
</form>

 

form.php

<?php

include("validation.php");
class form extends validation
{
   var $values = array();
   var $errors = array();
   var $num_errors;

   /* Class constructor */
   function form(){
      if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
         $this->values = $_SESSION['value_array'];
         $this->errors = $_SESSION['error_array'];
         $this->num_errors = count($this->errors);

         unset($_SESSION['value_array']);
         unset($_SESSION['error_array']);
      }
      else{
         $this->num_errors = 0;
      }
   }
   function setValue($field, $value){
      $this->values[$field] = $value;
   }
   
   function setError($field, $errmsg){
      $this->errors[$field] = $errmsg;
      $this->num_errors = count($this->errors);
   }

   function value($field){
      if(array_key_exists($field,$this->values)){
         return htmlspecialchars(stripslashes($this->values[$field]));
      }else{
         return "";
      }
   }

   function error($field){
      if(array_key_exists($field,$this->errors)){
         return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>";
      }else{
         return "";
      }
   }

   function getErrorArray(){
      return $this->errors;
   }
};

$form = new form;
?>

 

validation.php

<?php
if ($handle = opendir('validations')) {
    while (false !== ($file = readdir($handle))) {
    	if($file != "." && $file != "..")
    	{
        	include("validations/$file");
        }
    }
    closedir($handle);
}

class validation
{
var $valtype = array();
function validation()
{

}

function addvaltype($field, $valtype)
{
	$this->valtype[$field] = $valtype;
}

function validate()
{
	$i = 0;
	foreach ($this->valtype as $field => $val) 
	{
		$val->validate($_POST[$field], $field);
	}
}

}
?>

 

 

and an example of one of the validation classes

 

alphanum.php

<?php

// VALIDATION TYPE:	alphanum
// VALIDATION DESC:	only allow letters and numbers

class alphanum extends validation
{
function alphanum()
{
}

function validate($input, $field)
{
	if(!isset($input) || $input == "" || $input == NULL)
	{
		$this->setError($field, "$field cannot be left empty!");
		return 1;
	}
	elseif(!ctype_alnum($input))
	{
		$this->setError($field, "$field can only contain letters [a-z] and numbers [0-9]!");
		return 1;	
	}
	else
	{
		return 0;
	}
}

}

$alphanum = new alphanum;
?>

 

but when i submit the form i get this error: Fatal error: Call to a member function validate() on a non-object in C:\wamp\www\test\validation.php on line 30

 

line 30 is $val->validate($_POST[$field], $field);

 

any ideas?

i have a feeling that passing a variable as a class name is not legal and instead of trying to access the class

 

eg $val = alphanum;

 

instead of accessing $alphanum->validate() it is trying to access $val->validate?

So its about this class:

 

class validation {

var $valtype = array();  //var id PHP4; in PHP5 use public/protected/private

function validation() {  //in PHP5 constructor function is called __construct()

}

function addvaltype($field, $valtype) {  //also add visibility declarations (public/protected/private) to all functions 
	$this->valtype[$field] = $valtype;
}

function validate() {
	$i = 0;
	foreach ($this->valtype as $field => $val) {
		$val->validate($_POST[$field], $field);
	}
}
}

you use addvaltype() to add elements to $valtype array

 

$form->addvaltype("alphanumtest", "alphanum");
$form->addvaltype("digit test", "numeric");

 

Note: that you add strings

 

And then in function validate() you call function validate() on these strings...

 

foreach ($this->valtype as $field => $val) {
  $val->validate($_POST[$field], $field);
}

 

Well, strings are not objects. Hence the error.

i have a feeling that passing a variable as a class name is not legal and instead of trying to access the class

 

eg $val = alphanum;

 

instead of accessing $alphanum->validate() it is trying to access $val->validate?

 

Good thinking

 

You should use variable variables to pass class name in variable

$val = "alphanum";
$$val->validate();

 

Note however, that it will only work if object $alphanum is defined in current scope...

 

Sorry if I did not read through all the posts. But the original answer can be solved, I believe with this tidbit...

 

<?php
class b extends a
{

function b()
{
   a::b(); // execute class a function b;

   // or
   parent::b(); // execute the parent class (a) function's b.
}
}
?>

 

I am not 100% sure that will work, but yea.

Sorry if I did not read through all the posts. But the original answer can be solved, I believe with this tidbit...

 

code

 

I am not 100% sure that will work, but yea.

 

The question is: should it be solved at all? :P

 

:: operator should in general be used for calling static methods

Sorry if I did not read through all the posts. But the original answer can be solved, I believe with this tidbit...

 

code

 

I am not 100% sure that will work, but yea.

 

The question is: should it be solved at all? :P

 

:: operator should in general be used for calling static methods

 

You sure about that?

 

 

 

Example #3 Calling a parent's method

<?php

class MyClass

{

    protected function myFunc() {

        echo "MyClass::myFunc()\n";

    }

}

 

class OtherClass extends MyClass

{

    // Override parent's definition

    public function myFunc()

    {

        // But still call the parent function

        parent::myFunc();

        echo "OtherClass::myFunc()\n";

    }

}

 

$class = new OtherClass();

$class->myFunc();

?>

 

From the horses mouth at:

http://us.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

 

=)

That's special use :P

 

I'm not saying it won't work with non-static methods. I'm saying, that it is not necessarily the best way to do that.

 

The way I understand it, non-static methods should be associated with instantiated object. That's why calling parent::foo() from subclass is OK (it is either called from static method, or from non-static method of an object).

Now, I am confused.

 

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden members or methods of a class.

 

Is that not what he is wanting to do? Override the method of class a via class b? I think that was designed to allow overriding of functions inside of a class that has a parent...

 

EDIT:

I am not meaning to be rude or anything lol I am just confused =)

Not really. Read the topic :P

 

But in general, blueman wanted to have two or more classes ('b','c','d'), all of them extending class 'a'.

And then call for example method 'b()' of 'c' class from within class 'd'.

 

At least that's where it started :)

Not really. Read the topic :P

 

But in general, blueman wanted to have two or more classes ('b','c','d'), all of them extending class 'a'.

And then call for example method 'b()' of 'c' class from within class 'd'.

 

At least that's where it started :)

 

Ah my mistake then =)  Would having d extend class c and class c extend class b not work, I do not with PHP OOP since it is different than most, and never tried it...think I will right now.

 

 

<?php

class a
{
function a()
{
	echo 'BOOO!';
	echo "<br />";
}

function hyBrid() {
   echo "<br /> ORIGINAL HYBRID.";
}
}

class b extends a
{

function ab()
{
  echo 'RAWR!';
  echo "<br />";
}

function hyBrid() {
   echo "Hybrid in b ";
	parent::hyBrid();
}

function bb() {
   echo "BBBBBB";
   echo "<br />";
}
}

class c extends b
{

function hyBrid() {
   echo "Hybrid in c ";
	  parent::hyBrid();
}
function bc()
{
  echo "BCBCBC";
  echo "<br />";
}
}

class d extends c
{
function hyBrid() {
   echo "Hybrid in d ";
   parent::hyBrid();
}
function cd() {
	 parent::a();
}
}

$classD = new d();
$classD->ab(); // echos "RAWR!"
echo "<br /><br />";
$classD->bc(); 
echo "<br /><br />";
$classD->cd();
echo "<br /><br />";
$classD->bb(); 
echo "<br /><br />";
$classD->a(); 
echo "<br /><br />";
$classD->hyBrid(); // 
die();
?>

 

 

Expected output:

BOOO!
RAWR!


BCBCBC


BOOO!


BBBBBB


BOOO!


Hybrid in d Hybrid in c Hybrid in b
ORIGINAL HYBRID.

 

So yea.... I just did that part for my own needs =)

 

EDIT:

Just tabbed the code.

 

"When in doubt, test it out!" =)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.