TechnoDiver Posted August 15, 2021 Share Posted August 15, 2021 I'm looking for some insight on where/how to look something up. I have a Post class and it's really messy so I'm trying to clean it up. There's a method - createPost() - that takes in 8 parameters from a form, does what's needed to them to prepare them for database and creates 7 more properties, as well. For a total of 15 fields being sent to the database. I want to create a sendPost() method to handle this. In creating this method I realized that all these parameters look and feel burdensome and there's no real connection between the 2 methods other than sharing a class. I'd like to look into how to make sendPost take however many of parameters from whichever method it's called for without always having to pass the same amount. So for example if sendPost is being called with createPost than it has to handle 15 parameters, but, later, if I want to call sendPost on an editPost method, for example, it may only need to send 6 parameters back to the database meaning the other 9 would have to be redundantly passed again. I seem to recall from Python years ago there was a way to do this but I can't remember and I really don't know what to even search for to research it. Any direction or resources or advice that anyone could share would be really helpful and appreciated. Thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted August 15, 2021 Share Posted August 15, 2021 Send an array of parameters instead of lots of individual ones, or you can use the splat (ellipsis) operator (see variadic functions here) Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted August 17, 2021 Author Share Posted August 17, 2021 Ok, thanks for that link interesting read I definitely see where I can make use of that. Let me ask you something though, and I think I can explain without posting too much code. I have a Post class. In it there are multiple methods for displaying different types of posts. Column names from the database are assigned to property variables and the each method loops through a while loop and echos the appropriate html with the variable/properties ($title, $content, $image etc) inserted in the HTML for where the post is located on the site. It was sloppy and dirty and I needed to clean it up so I've made a Display class whose methods each only echo the relevant html and nothing else. Now as I've moved the HTML from the Post class to the Display class those variables that are inserted in the HTML have become a problem. Not a problem but an issue I have to deal with. In the Display class they all have to red squiggly error lines because they're not defined in that class. The thing is that the Display class doesn't really use them, they're only ever actually used in the Post class because that's where they're initiated. So I don't know the best way to deal with them as defining them all again in Display seems really redundant and doesn't feel right. Is there a way to 'escape' them while they're in the Display class and only get defined once they've made it to the Post class? Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 17, 2021 Share Posted August 17, 2021 It sounds like something's confused in what you're doing. Are you calling the Display class now instead of the Post class? And either way, if your IDE is giving you red squigglies in the Display class then Display clearly does use the variables. It sounds like you want to still call the Post class as the instigator, then call from the Post class to the Display class for output - this way your interface to the overall system doesn't change, and you can only pass the parameters that are needed to the Display class methods. It also allows you to swap out Display classes is you need to at some point, as long as the interface is consistent. Post some actual code and we can take a look. Quote Link to comment Share on other sites More sharing options...
TechnoDiver Posted August 17, 2021 Author Share Posted August 17, 2021 (edited) 3 hours ago, maxxd said: It sounds like you want to still call the Post class as the instigator, then call from the Post class to the Display class for output Yea, this is exactly what I'm trying. So here's one example method -> public function getBreakingNews() { $query = mysqli_query($this->conn, "SELECT * FROM news WHERE type='breaking' ORDER BY RAND()"); $str = ""; while($row = mysqli_fetch_array($query)) { $id = $row['id']; $title = $row['title']; if(strlen($title) > 25) { $title = substr($title, 0, 25) . "..."; } $content = $row['content']; // if(strlen($content) > 200) { // $content = substr($content, 0, 200) . "..."; // } $added_by = $row['add_by']; $category = ucwords($row['post_category']); $cat_id = $row['post_cat_id']; $image = $row['post_image']; $likes = $row['num_likes']; $comments = $row['num_comments']; $date_added = $row['date_added']; $current_datetime = date("Y-m-d H:i:s"); $start_count = new DateTime($date_added); $end_count = new DateTime($current_datetime); $interval = $start_count->diff($end_count); if($interval->h <= 8 && $interval->d < 1) { $str .= " <div class='single-blog-post small-featured-post d-flex'> <div class='post-thumb'> <a href='single_post.php?post_id=$id&related=$category'><img src='img/posts/$image' alt=''></a> </div> <div class='post-data'> <a href='category_posts.php?cat_id=$cat_id&cat_title=$category' class='post-category text-size-6'>$category</a> <div class='post-meta'> <a href='single_post.php?post_id=$id&related=$category' class='post-title'> <h6>$title</h6> </a> <p class='post-date'><span>$date_added</span></p> </div> </div> </div>"; } } It's ugly, I know, but it's first draft code from a first draft coder. It's also redundant. Initially all these methods are assigning properties from the db query $row array. Now I've made it so any data coming from the form is in Post __construct(). This was in response to reading about the double colon to use properties from other classes. What I'm working on now is just moving the HTML echo from $str into a Display class. You can see where there are the PHP variables with the HTML. When I copy this HTML over to the Display class it doesn't have immediate access to those variable definitions anymore, so they're all considered unassigned. Here's a portion of the Display class with the corresponding method for the HTML above.as I was working on it last night -> require("Post.php"); class Display { private $classname = "Post"; private $title = $classname::$title; private function __construct($loc_code) { $this->loc_code = $loc_code; } //loc_code #1 private function breakingDisplay() { $html = "<div class='single-blog-post small-featured-post d-flex'> <div class='post-thumb'> <a href='single_post.php?post_id=$id&related=$category'><img src='img/posts/$image' alt=''></a> </div> <div class='post-data'> <a href='category_posts.php?cat_id=$cat_id&cat_title=$category' class='post-category text-size-6'>$category</a> <div class='post-meta'> <a href='single_post.php?post_id=$id&related=$category' class='post-title'> <h6>$title</h6> </a> <p class='post-date'><span>$date_added</span></p> </div> </div> </div>"; echo $html; } The methods are private because there will be only one Public method in Display - the one that chooses which Private method to use depending on which $loc_code is passed. Here, I've only yet tried assigning the $title property. The line private $title = $classname::$title; tells me $title has been assigned but not used, but it's clearly used in the HTML. I imagine the same thing will happen with the rest so haven't spent time on them yet. This is what I'm working on now. My original question was slightly different. Originally I was asking if it was possible to 'escape' the HTML variables in Display until they needed to be used in Post. So that's my situation right now. I love advise and guidance and appreciate it all. What I really need is a good resource on moving data between objects and classes, this is something I don't yet have total comfort with, if anyone has any links/resources like that. Edited August 17, 2021 by TechnoDiver Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 18, 2021 Share Posted August 18, 2021 There are some fundamentals that you need to understand. Take a look at variable scope and how to pass variables from one function/method to another, visibility and how it affects properties and methods, and static methods and properties. Once you work on that you can look at the pattern you're using - right now you're going from the outside in. You're calling to the display functions and asking them to include the functions that do the work. You want to call the functions that do the work, store the result of that work, then pass that to the display class. 1 Quote Link to comment 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.