Trying to call a protected function from outside the class

半城伤御伤魂 提交于 2020-03-05 07:25:41

问题


OK. I'm new to Classes in PHP and trying to pass variables into the protected functions in the class. How do I do this?

CLASSES.PHP

<?php
    include($_SERVER['DOCUMENT_ROOT']."/includes/con.php");

    class gindex {
        protected function rdev($a,$b,$c,$d){
            $d = base64_encode($d);
            mysql_query("INSERT INTO mem(first_name,last_name,email,password,type) VALUES(".$a.",".$b.",".$c.",".$d.",'developer')", $db);
    }
?>

INDEX.PHP

<?php
    include($_SERVER['DOCUMENT_ROOT']."/includes/con.php");
    if(isset($_POST['developerbtn'])){
        $fname = $_REQUEST['fname'];
        $lname = $_REQUEST['lname'];
        $email = $_REQUEST['email'];
        $password = $_REQUEST['password'];
        $Cgindex = new gindex();
        $Cgindex->rdev($fname,$lname,$email,$password);
    }
?>

回答1:


You cannot do that because the method is set to protected you have to set it to public to be able to access it. Otherwise you can only call it from the same class or the children of that class.

May I also suggest to start your classname with an uppercase: class Gindex. You may also want to improve the name of the class because gindex says nothing of what it does. Same goes for your method name. And your parameters names are also terrible. Name things correctly so people (including) yourself will know exactly what a variable contains / class or method does when they are (re)viewing your code.

You are also using a variable $db which isn't defined anywhere in the scope of the class.

Also please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is a good PDO tutorial.

This will also fix that nasty SQL Injection you have there in your code. For more information about how to fix this with either PDO or mysqli see this question.

It also looks like you are using the website as the password when inserting the data into the database. If you are really going to store database I also suggest you read into hashing the password for security.




回答2:


I suggest you read the docs on visibility again.

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

$Cgindex->rdev(... is calling from the global context, not within an allowed class context.




回答3:


You can't, the purpose of a protected function is to be called only from inside the class, or an inherited class

so make your function public, if you wanna be able to call it like you did:

public function rdev($a,$b,$c,$d){



回答4:


You need to read about OOP in general.
A protected function is ... protected from outside the class use. Means: Only code inside methods of class, or child classes, can call the protected method. Also, all child classes of the class holding the protected method, will inherit that method.




回答5:


This should help:

Public — A public variable or method can be accessed directly by any user of the class.

Protected — A protected variable or method cannot be accessed by users of the class but can be accessed inside a subclass that inherits from the class.

Private — A private variable or method can only be accessed internally from the class in which it is defined.This means that a private variable or method cannot be called from a child that extends the class.




回答6:


Protected elements are accessible only from the same class which were defined and their subclasses not from outside.




回答7:


function __construct(){
		$this->rdev();
	}

Initialize in Construct..



来源:https://stackoverflow.com/questions/12255740/trying-to-call-a-protected-function-from-outside-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!