blueman378 Posted December 11, 2008 Share Posted December 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/ Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 b,c,d are all subclasses of a, so they only see their own function b() Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712320 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 so to access them would i have to create an indervidual instance of the and call them through eg $d = new d; $d->b(); ? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712323 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 Yes. Unless you define these functions as static, then they're available without instantiating an object. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712325 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712334 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712341 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712342 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712347 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 hmm, now i need to figure out a way to define it inside the scope... because i dont know the names of the classes because the names come from the files that are chucked in a directory... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712354 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 You should use addvaltype to add a reference to actual object, not its name I think Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712362 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 howso would i go about doing that? Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712368 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 Not sure really... I'm just learning OOP patterns myself BTW: Which PHP version you use? There are quite large differences in OOP between PHP4 and PHP5 Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712379 Share on other sites More sharing options...
waterssaz Posted December 11, 2008 Share Posted December 11, 2008 forgive my ignorance. But I'm struggling to see in your code where exactly you intiated $val as a new instance of the validation class. Therfore $val->validate() would not have any relevance. :-) Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712397 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 well i got it working finially, but im too tired. i have a feeling some of my extends X are creating a loop but ill looki into that tomorrow when im more awake. cheers guys. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712403 Share on other sites More sharing options...
redarrow Posted December 11, 2008 Share Posted December 11, 2008 OOP only supported from php 5 ((A upgrade is needed )) i guess. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712410 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 OOP only supported from php 5 ((A upgrade is needed )) i guess. Real OOP that is. There were some OO elements in PHP4. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712412 Share on other sites More sharing options...
blueman378 Posted December 11, 2008 Author Share Posted December 11, 2008 na we are running php 5.2.8 Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712934 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712939 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 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? :: operator should in general be used for calling static methods Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712954 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 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? :: 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712958 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 That's special use 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). Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712967 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712975 Share on other sites More sharing options...
Mchl Posted December 11, 2008 Share Posted December 11, 2008 Not really. Read the topic 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 Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712990 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 Not really. Read the topic 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. Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-712999 Share on other sites More sharing options...
premiso Posted December 11, 2008 Share Posted December 11, 2008 <?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!" =) Quote Link to comment https://forums.phpfreaks.com/topic/136470-some-help-with-class-extensions/#findComment-713016 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.